首頁 ›
部落格 › Microsoft MarkItDown 繁中教學
🏢 Microsoft 官方開源 — 本週 GitHub 週榜 #2
Microsoft MarkItDown 繁中完整教學 2026:146K Stars,PDF/Office/HTML 一鍵轉 Markdown,RAG 前處理神器
一行 pip 安裝,20+ 格式統一轉 Markdown。讓你的 AI 應用能讀取任何企業文件——再也不用為格式轉換煩惱。
📅 2026年6月7日
🏷️ Microsoft / RAG / Python / AI 工具
⏱️ 閱讀約 9 分鐘
✍️ AutoDev AI 編輯部
MarkItDown 是什麼?為什麼 146K Stars 是實力認證
MarkItDown 是 Microsoft 開源的多格式文件轉 Markdown 工具,由 Microsoft AutoGen 團隊開發維護,目前在 GitHub 累積 146,378 Stars,本週週榜穩居第二,已連續多週保持高熱度。
聽起來很簡單——把文件轉成 Markdown。但這件事在 AI 應用開發中非常關鍵:
核心問題:AI 模型讀不了 PDF。你的公司有大量 Word 文件、Excel 報表、PowerPoint 簡報,這些都是 LLM 無法直接理解的格式。MarkItDown 是橋樑:把這些格式轉換為 AI 友好的 Markdown,讓你的 RAG 系統能夠真正「讀懂」企業知識庫。
為什麼不直接用 pypdf 或 python-docx?
- 統一介面:一套 API 處理所有格式,不需要對每種格式寫不同的解析邏輯
- 結構保留:MarkItDown 保留文件的標題層級、表格、列表結構,而不是輸出混亂的原始文字
- AI 優化輸出:輸出的 Markdown 格式對 text chunking 和 embedding 更友好
- Microsoft 官方維護:企業環境的信任度和長期維護保障
- 持續擴展:社群不斷新增格式支援,目前已超過 20 種
安裝與快速上手(3分鐘)
基礎安裝
pip install markitdown
pip install "markitdown[all]"
markitdown --version
30 秒快速體驗
from markitdown import MarkItDown
md = MarkItDown()
result = md.convert("company-report.pdf")
print(result.text_content)
result = md.convert("meeting-notes.docx")
print(result.text_content)
with open("output.md", "w", encoding="utf-8") as f:
f.write(result.text_content)
Python API 完整使用指南
URL 直接轉換(爬取網頁)
from markitdown import MarkItDown
md = MarkItDown()
result = md.convert("https://autodev-ai.com/blog/")
print(result.text_content)
result = md.convert("https://zh.wikipedia.org/wiki/人工智慧")
批次轉換多個檔案
from markitdown import MarkItDown
from pathlib import Path
md = MarkItDown()
input_dir = Path("./documents/")
output_dir = Path("./markdown/")
output_dir.mkdir(exist_ok=True)
supported_extensions = [".pdf", ".docx", ".xlsx", ".pptx", ".html"]
for file_path in input_dir.rglob("*"):
if file_path.suffix.lower() in supported_extensions:
try:
result = md.convert(str(file_path))
output_file = output_dir / (file_path.stem + ".md")
output_file.write_text(result.text_content, encoding="utf-8")
print(f"✅ 轉換成功: {file_path.name}")
except Exception as e:
print(f"❌ 轉換失敗: {file_path.name} — {e}")
圖片 OCR(搭配 GPT-4o)
from markitdown import MarkItDown
from openai import OpenAI
client = OpenAI()
md = MarkItDown(
llm_client=client,
llm_model="gpt-4o"
)
result = md.convert("invoice-scan.png")
print(result.text_content)
命令行工具使用教學
markitdown report.pdf
markitdown report.pdf -o report.md
markitdown https://github.com/microsoft/markitdown
for f in *.pdf; do markitdown "$f" -o "${f%.pdf}.md"; done
markitdown company-report.pdf | claude "幫我摘要這份報告的重點"
RAG Pipeline 實戰整合
這是 MarkItDown 最有商業價值的應用場景。以下是一個完整的企業文件 RAG 系統建置範例:
1
文件收集與轉換
使用 MarkItDown 將企業知識庫(Word/PDF/Excel)轉換為統一的 Markdown 格式
2
文字切片(Chunking)
按照 Markdown 標題結構切分,每個切片 512-1024 tokens,保留語義完整性
3
向量化(Embedding)
使用 OpenAI text-embedding-3-small 或本地模型轉換為向量
4
向量資料庫存儲
使用 Chroma、Qdrant 或 pgvector 存儲向量索引
5
RAG 查詢
用戶問題向量化 → 語義搜尋 → 取出相關片段 → 注入 LLM context → 生成回答
LlamaIndex 整合範例
from markitdown import MarkItDown
from llama_index.core import VectorStoreIndex, Document
from pathlib import Path
import os
md = MarkItDown()
documents = []
for file_path in Path("./knowledge-base/").rglob("*"):
if file_path.is_file():
result = md.convert(str(file_path))
if result.text_content.strip():
documents.append(
Document(
text=result.text_content,
metadata={
"filename": file_path.name,
"source": str(file_path)
}
)
)
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("我們的退換貨政策是什麼?")
print(response)
進階功能:OCR、音訊轉錄、批次處理
ZIP 批次轉換
from markitdown import MarkItDown
md = MarkItDown()
result = md.convert("company-documents-2026.zip")
print(result.text_content)
SharePoint 整合(Microsoft 365 環境)
from markitdown import MarkItDown
from markitdown.converters.sharepoint import SharePointConverter
md = MarkItDown()
result = md.convert(
"https://yourcompany.sharepoint.com/sites/docs/report.docx"
)
自訂 Converter 擴展
from markitdown import MarkItDown, DocumentConverter, DocumentConverterResult
class MyCustomConverter(DocumentConverter):
def convert(self, local_path, **kwargs) -> DocumentConverterResult:
with open(local_path, "r") as f:
content = f.read()
return DocumentConverterResult(
title="Custom File",
text_content=f"# Custom Format\n\n{content}"
)
md = MarkItDown()
md.register_converter(MyCustomConverter(), priority=0)
MarkItDown vs 同類工具比較
| 工具 |
格式支援 |
輸出格式 |
AI 優化 |
維護者 |
最適場景 |
| MarkItDown ⭐ |
20+ 種 |
Markdown |
✅ 原生優化 |
Microsoft 官方 |
RAG 前處理、統一文件轉換 |
| pypdf |
PDF only |
純文字 |
❌ |
社群 |
只需要處理 PDF |
| python-docx |
DOCX only |
Python 物件 |
❌ |
社群 |
需要操作 Word 文件內容 |
| Unstructured |
多種 |
多種 |
⚠️ 部分 |
Unstructured.io |
需要更精細的文件元素提取 |
| Pandoc |
40+ 種 |
多種 |
❌ |
學術社群 |
文件格式互轉(非 AI 專用) |
| liteparse |
PDF 優先 |
Markdown |
✅ |
LlamaIndex 官方 |
高效能 PDF 解析(Rust) |
建議搭配:對於大量 PDF 的場景,MarkItDown + liteparse 是最強組合——MarkItDown 處理 Word/Excel/PPT/HTML 等多格式,liteparse(LlamaIndex 官方 Rust 解析器)負責高速 PDF 處理,兩個工具互補,覆蓋所有企業文件格式。
FAQ:台灣工程師常見問題
Q1: MarkItDown 轉換繁中 PDF 的效果如何?
表現良好。MarkItDown 使用 pdfminer.six 作為 PDF 後端,對繁中(正體中文)的支援度與英文相近。但需注意:掃描版 PDF(非文字 PDF)需要啟用 OCR 功能(需要 GPT-4o 視覺)。內嵌字型的 PDF 可能在極少數情況下出現亂碼,此時建議改用 liteparse。
Q2: MarkItDown 轉換 Excel 表格的格式是什麼?
MarkItDown 會將 Excel 的每個工作表(Sheet)轉換為 Markdown 表格格式(使用 | 分隔符)。大型表格(超過 100 行)建議先用 pandas 預先篩選資料再轉換,避免 context 過長。
Q3: 轉換後的 Markdown 可以直接給 Claude 或 GPT 用嗎?
可以。MarkItDown 輸出的 Markdown 格式對 LLM 非常友好,標題層級、表格結構都有正確的 Markdown 語法標記。實際使用時,建議搭配 chunking 工具(如 LlamaIndex 的 SentenceSplitter)按 1024 tokens 切片,避免一次塞入過長的文件。
Q4: MarkItDown 支援 Google Docs / Google Sheets 嗎?
目前 MarkItDown 不直接支援 Google Workspace 格式。但有兩種解決方案:1. 從 Google Drive 下載為 .docx / .xlsx 後再轉換。2. 使用 Google Drive API 配合 MarkItDown,自動化下載和轉換流程。
Q5: MarkItDown 和 Claude Code 怎麼搭配?
最佳搭配方式:在 Claude Code 的工作區中安裝 MarkItDown,當你需要分析 PDF 文件或讀取 Excel 數據時,先用 MarkItDown 轉換,再把 Markdown 結果注入 Claude Code 的 context。這樣不需要 Claude Code 直接讀取二進位格式,效率更高且 token 使用更可預測。