內建 converter 全部在本機離線運作。需要更高品質(掃描 PDF、複雜表格、影片、結構化欄位)時,
MarkItDown 提供兩條 Azure 雲端路徑。
兩者對照
能力 內建 converter Document Intelligence Content Understanding
文件轉換 離線、格式特定抽取 雲端版面分析 雲端多模態抽取
結構化欄位 不支援 此整合未暴露 YAML front matter(analyzer 欄位)
自訂 analyzer 不支援 此整合不可設定 cu_analyzer_id 支援
音訊 / 影片 僅基本音訊、無影片 不支援 音訊 + 影片 analyzer
成本 純本機 Azure API 計費 Azure API 計費
何時用 Content Understanding
音訊與影片 ——CU 是影片的唯一選項、音訊的高品質雲端選項。
結構化欄位抽取 ——預建或自訂 analyzer 抽出發票金額、收據日期、合約條款,序列化成 YAML front matter。
高品質文件抽取 ——掃描 PDF、複雜表格、多頁文件的雲端版面分析。
單一 API 涵蓋所有模態 ——一個 cu_endpoint 自動路由文件/圖/音/影。
Content Understanding 用法
pip install 'markitdown[az-content-understanding]'
# CLI
markitdown report.pdf --use-cu --cu-endpoint "<endpoint>"
# Python(零設定,自動依檔案類型選 analyzer)
from markitdown import MarkItDown
md = MarkItDown(cu_endpoint="<endpoint>")
print(md.convert("report.pdf").markdown) # 文件 → prebuilt-documentSearch
print(md.convert("meeting.mp4").markdown) # 影片 → prebuilt-videoSearch
# 自訂 analyzer(領域欄位抽取)
md = MarkItDown(cu_endpoint="<endpoint>", cu_analyzer_id="my-invoice-analyzer")
print(md.convert("invoice.pdf").markdown)
# 輸出含 YAML front matter:
# ---
# contentType: document
# fields:
# VendorName: CONTOSO LTD.
# InvoiceDate: '2019-11-15'
# ---
# <!-- page 1 -->
計費與範圍控制
計費注意 :每個 convert() 呼叫,只要是 CU 路由的格式,
就是一筆可計費的 Azure API 呼叫 。用 cu_file_types 限制哪些格式才走 CU:
from markitdown.converters import ContentUnderstandingFileType
md = MarkItDown(
cu_endpoint="<endpoint>",
cu_file_types=[ContentUnderstandingFileType.PDF], # 只有 PDF 走 CU
)
Document Intelligence 用法
pip install 'markitdown[az-doc-intel]'
# CLI
markitdown file.pdf -o document.md -d -e "<document_intelligence_endpoint>"
# Python
md = MarkItDown(docintel_endpoint="<document_intelligence_endpoint>")
result = md.convert("test.pdf")
print(result.text_content)
本站說明
Azure 兩條路徑都需要雲端資源與計費,本站不實跑 ;以上內容為官方 README 的忠實翻譯與對照整理。
建立資源的指引見 Document Intelligence
與 Content Understanding 官方文件。
概念解說
Azure 雲端路徑解決內建 converter 的兩個限制:(1) 掃描版 PDF / 複雜表格的版面分析、
(2) 音訊 / 影片的結構化抽取。兩條路徑:Document Intelligence (文件專用)和
Content Understanding (多模態,涵蓋文件/圖/音/影)。
Content Understanding 的獨特價值:結構化欄位抽取 (發票金額、合約條款)會序列化成 YAML front matter,
以及自訂 analyzer 支援特定領域的欄位定義。
實際操作 Worked Example
步驟 1:安裝 Azure 依賴
# Content Understanding
pip install 'markitdown[az-content-understanding]'
# 或 Document Intelligence
pip install 'markitdown[az-doc-intel]'
步驟 2:用 CU 轉換掃描 PDF
from markitdown import MarkItDown
md = MarkItDown(cu_endpoint="https://<resource>.cognitiveservices.azure.com/")
result = md.convert("scanned_report.pdf")
print(result.markdown) # 含 YAML front matter + 版面分析結果
步驟 3:用自訂 analyzer 抽取發票欄位
md = MarkItDown(
cu_endpoint="<endpoint>",
cu_analyzer_id="my-invoice-analyzer",
)
result = md.convert("invoice.pdf")
# 輸出含:
# ---
# contentType: document
# fields:
# VendorName: CONTOSO LTD.
# InvoiceDate: '2019-11-15'
# TotalAmount: 1234.56
# ---
常見錯誤與除錯
每個 convert() 都是計費呼叫
用 cu_file_types 限制哪些格式才走 CU,避免意外計費。例如只讓 PDF 走 CU:
cu_file_types=[ContentUnderstandingFileType.PDF]
認證失敗
確認 Azure 資源的端點 URL 正確、API Key 已設定(環境變數或參數傳入)。
練習與驗收
建立 Azure Content Understanding 資源,取得端點和 API Key。
用預建 analyzer 轉換一份掃描 PDF,觀察 YAML front matter 輸出。
用 cu_file_types 限制只有 PDF 走 CU,測試 XLSX 是否仍用內建轉換。
① 進階真實情境:本機+雲端混和路由 + 成本控管 + 品質閘
場景:你的文件庫有「電子 PDF」與「掃描 PDF」兩種。電子 PDF 內建 converter 就夠好且免費;
掃描 PDF 內建會「假成功」出空字串。目標:只讓該上雲的檔案上雲,其餘留本機 ——品質與成本兼顧。
from pathlib import Path
from markitdown import MarkItDown
from markitdown.converters import ContentUnderstandingFileType
# 策略:CU 只服務 PDF;其餘格式(docx/xlsx/mp4)看 cu_file_types 決定
md = MarkItDown(
cu_endpoint="https://<resource>.cognitiveservices.azure.com/",
cu_file_types=[ContentUnderstandingFileType.PDF], # 只讓 PDF 走 CU
)
for pdf in Path("scans/").glob("*.pdf"):
text = md.convert_local(str(pdf)).markdown
# 品質閘:掃描 PDF 若內建抽出太少,補一次 CU
if len(text.strip()) < 100:
text = md.convert_local(str(pdf), use_content_understanding=True)
print(f"{pdf.name}: {len(text)} chars")
為什麼選這條路徑 :全走 CU 會讓每份文件都計費;全走內建則掃描件品質崩盤。
用 cu_file_types 建立「預設本機、指定格式上雲 」的路由,
再用字數品質閘做補償性升級 ——先免費試,不夠好才付費重轉。這是把成本控制變成可執行策略的標準做法。
② 深入原理擴充:CU 的 analyzer 自動路由與 YAML front matter
Content Understanding 的「零設定」背後其實有一套自動路由邏輯 :
依檔案的型別偵測結果,自動挑 analyzer:文件 → prebuilt-documentSearch、影片 → prebuilt-videoSearch。
可以 cu_analyzer_id 指定自訂 analyzer,抽出領域欄位(發票金額、收據日期…)。
結構化欄位會序列化成YAML front matter 放在 Markdown 開頭,讓 LLM 直接讀到欄位值。
大家以為轉換沒問題但其實丟資訊
YAML front matter 只在 analyzer 有定義欄位時才出現 。用 prebuilt-documentSearch
轉一份含「金額表格」的文件,輸出是版面文字——不是結構化欄位,因為文件 analyzer 不抽發票欄位。
很多人以為「用了 CU 就有欄位」,其實欄位是 analyzer 的定義,不是自動的 ;
要拿欄位得用自訂 analyzer。若你的下游依賴 YAML fields,務必先確認 analyzer 有定義。
③ 診斷式疑難排解表
症狀 可能原因 解決方案
CU 轉換後沒有 YAML front matter 使用的 analyzer 未定義那些欄位 改用自訂 analyzer 或預建的 documentFields 系列
某些格式沒走 CU(該收費的沒收費) cu_file_types 未包含該格式檢查 ContentUnderstandingFileType 白名單
帳單暴增 太多格式走 CU 縮小 cu_file_types;先本機後補償升級
-d 與 --use-cu 無法同時用兩條 Azure 路徑互斥 擇一;同一次轉換只能用一條雲端服務
認證失敗(401/403) 端點 URL 或 API Key 錯誤 確認資源端點、設定環境變數或傳入參數
④ 進階挑戰題
設計一份「成本控管策略」:列出 5 種格式,各標註「走內建 / 走 CU」,並預估每月 API 呼叫數與費用。
實作「補償升級」管線:本機轉換 → 品質評分 → 低分才送 CU,並記錄每次升級的次數與成本。
比較同一份掃描 PDF 用「內建」「Document Intelligence」「Content Understanding」三種輸出的差異,列出各自擅長與丟失的資訊。
⑤ 專案級端到端 Worked Example:多來源文件擷取管線 + 智慧路由 + 成本追蹤
場景:你的團隊要把來自三個來源的文件(FTP 機器上的歷史 PDF、S3 的掃描收據、SharePoint 的 Office 文件)
全部轉成 Markdown 進向量資料庫。每種來源有不同的格式分佈——PDF 多掃描件、Office 多電子版。
你要建一條智慧路由管線 :電子版走本機、掃描版走 CU、其他格式跳過雲端——
並在每次轉換後記錄「花多少錢、省多少錢」。
# 1. 三來源擷取 + 統一路由(routing_pipeline.py)
import boto3, os, tempfile
from pathlib import Path
from markitdown import MarkItDown
from markitdown.converters import ContentUnderstandingFileType
md = MarkItDown(
cu_endpoint=os.environ["CU_ENDPOINT"],
cu_file_types=[ContentUnderstandingFileType.PDF],
)
def route_and_convert(path: Path) -> dict:
"""智慧路由:電子 PDF 走本機,掃描 PDF 走 CU,Office 走本機,其他跳過。"""
ext = path.suffix.lower()
if ext not in (".pdf", ".docx", ".xlsx", ".pptx"):
return {"path": str(path), "status": "skipped", "chars": 0}
if ext == ".pdf":
# 先試本機
local_text = md.convert_local(str(path)).markdown
if len(local_text.strip()) >= 100:
return {"path": str(path), "status": "local", "chars": len(local_text),
"cost": 0}
# 品質不足 → CU 升級
cu_text = md.convert_local(str(path), use_content_understanding=True).markdown
return {"path": str(path), "status": "cu", "chars": len(cu_text),
"cost": estimate_cu_cost(path)}
# Office 格式一律本機
text = md.convert_local(str(path)).markdown
return {"path": str(path), "status": "local", "chars": len(text), "cost": 0}
def estimate_cu_cost(path: Path) -> float:
"""簡易計費估算:CU 每頁約 $0.01(示意值)"""
try:
import pdfplumber
with pdfplumber.open(str(path)) as pdf:
return len(pdf.pages) * 0.01
except Exception:
return 0.05 # 預估
# 2. 批次執行 + 統計
results = []
for pdf in Path("scans/").rglob("*.pdf"):
results.append(route_and_convert(pdf))
for docx in Path("office/").rglob("*.docx"):
results.append(route_and_convert(docx))
local_count = sum(1 for r in results if r["status"] == "local")
cu_count = sum(1 for r in results if r["status"] == "cu")
total_cost = sum(r.get("cost", 0) for r in results)
print(f"本地: {local_count} CU 升級: {cu_count} 總花費: ${total_cost:.2f}")
# 預期輸出
本地: 1,847 CU 升級: 153 總花費: $1.53
# 沒有 cu_file_types 的話:全部 2,000 份都走 CU → $20.00(13x 成本)
為什麼這是「整專案規模」 :第 1 輪是「本機+CU 混和路由」單一檔案;
這裡是多來源、多格式、自動路由、成本追蹤的完整管線 ——
cu_file_types 讓「只有掃描 PDF 才走 CU」成為可執行策略,而不是口號。
成本追蹤讓「雲端值不值得」變成可量測的數字。
⑥ 效能/品質/安全深度:雲端轉換的三面考量
效能 :CU 每次呼叫有 cold start 延遲(首次約 2-5 秒),
批次處理時可用連線池 降低;大檔(>50 頁)考慮分段上傳 避免逾時。
用 cu_file_types 過濾可減少不必要的 API 呼叫——每過濾一個格式就省一筆請求。
品質 :CU 的結構化欄位 只在 analyzer 有定義時才出現——
用預建 prebuilt-documentSearch 只有版面文字,沒有欄位;
要拿發票金額/合約條款,必須用自訂 analyzer。很多人「用了 CU 但沒有欄位」是因為 analyzer 選錯。
安全 :文件內容上傳到 Azure——資料離開你的網路邊界 。
需確認:(1) Azure 資源的資料保留政策(預設 24 小時),(2) 是否需要 VNET 介面卡隔離,
(3) 敏感文件(個資/財務)是否該走本機而非雲端。計費端點的 API Key 不能出現在前端或版本控制。
⑦ 文件間比較對照表:雲端 vs 相關頁
面向 本文(Azure 雲端轉換) 相關文(Python API/轉換器/安全模型) 差異說明
路徑選擇 Document Intelligence vs Content Understanding 兩條 python-api 講 5 個 convert_* 入口;converters 講 22 個內建 本文是「雲端升級路徑」,其餘是「本機基礎」
計費 每次 CU 呼叫計費 + cu_file_types 篩選 本機無計費;docker 是固定成本 本文有「每次呼叫成本」,其餘是「環境成本」
品質上限 掃描 PDF、複雜表格、影片的最高品質路徑 converters 列出內建限制;how-it-works 講假成功 本文是「補上限制」,其餘是「指出限制在哪」
安全邊界 資料離開本機 → Azure 雲端 security 頁講信任邊界;docker 頁講容器隔離 本文的安全是「資料外流」,其餘是「輸入消毒」
⑧ 互動式檢核清單:進階驗收