該選哪個函式庫
| 函式庫 | 授權 | 需要 | 最適合 |
|---|---|---|---|
| PyMuPDF4LLM | AGPL 3.0,或付費商業授權 | 只要 pip | 快速準確地處理文字版 PDF,標題和表格 |
| MarkItDown | MIT | 只要 pip | 一次呼叫處理多種檔案格式 |
| Docling | MIT | PyTorch,需下載模型 | 複雜版面、表格、結構化輸出 |
| Marker | 程式碼為 Apache 2.0,模型權重為修改版 OpenRAIL-M 授權 | PyTorch,以及 OCR 用的推論伺服器 | 公式多的論文和掃描檔,最好有 GPU |
下面的版本是本頁頂端日期時的最新版。如果你只有幾個 PDF,這些都用不到:我們首頁的轉換器在瀏覽器裡就能做同樣的事。
PyMuPDF4LLM
PyMuPDF 的 Markdown 匯出功能。從 PyMuPDF 2.0 起它已包含在 pymupdf 套件裡,匯入名稱仍是 pymupdf4llm。
pip install pymupdf
import pymupdf4llm
md = pymupdf4llm.to_markdown("report.pdf")
open("report.md", "w", encoding="utf-8").write(md)
常用選項:
# 只轉換部分頁面(從 0 開始),每頁回傳一個帶中繼資料的 dict
pages = pymupdf4llm.to_markdown("report.pdf", pages=[0, 1, 2], page_chunks=True)
for p in pages:
print(p["metadata"]["page_number"], len(p["text"]))
處理掃描頁還要裝一個 OCR 引擎:執行 pip install rapidocr onnxruntime 後,PyMuPDF 就能讀取沒有文字層的頁面。
授權: PyMuPDF 採雙重授權,可選 GNU AGPL 3.0 或 Artifex 的商業授權。AGPL 要求你在散布軟體或以網路服務形式提供時公開你自己的原始碼,所以用在閉源產品之前請先確認。
在我們的實測裡,PyMuPDF4LLM 2.1 平均保留了 99% 的正文,閱讀順序是三個參測轉換器中最準的。
MarkItDown
微軟的多格式轉換器,支援 PDF、Word、PowerPoint、Excel、HTML、EPUB 等。
pip install 'markitdown[pdf]'
from markitdown import MarkItDown
md = MarkItDown()
result = md.convert("report.pdf")
open("report.md", "w", encoding="utf-8").write(result.markdown)
命令列用法:markitdown report.pdf -o report.md。
授權: MIT。
需要知道: 處理 PDF 時它用 pdfminer.six 和 pdfplumber 擷取文字。輸出沒有標題,保留 PDF 的換行和行尾連字號,預設安裝也沒有 OCR。如果 PDF 只是一大堆混雜辦公檔案中的一小部分,選它就對了。詳情見我們的 MarkItDown 頁面。
Docling
一個起步於 IBM 蘇黎世研究院、現由 LF AI & Data 基金會託管的文件轉換工具包。它執行版面、表格和 OCR 模型,所以會安裝 PyTorch,第一次使用時下載模型。除了 PDF,它還能讀取 Word、PowerPoint、Excel、HTML、圖片等。
pip install docling
from docling.document_converter import DocumentConverter
converter = DocumentConverter()
result = converter.convert("report.pdf")
md = result.document.export_to_markdown()
open("report.md", "w", encoding="utf-8").write(md)
命令列用法:docling report.pdf,會在目前資料夾寫出一個 .md 檔。
授權: MIT。除了 Markdown,文件還能匯出為 HTML,以及一種保留版面資訊、方便後續處理的無損 JSON 格式。
Marker
Datalab 的轉換器,圍繞自家的視覺模型打造。擅長公式和掃描檔,執行起來也比較重。
pip install marker-pdf
Marker 需要 PyTorch,做 OCR 時第一次使用會啟動一個推論伺服器:NVIDIA GPU 上用 vLLM,CPU 或 Apple Silicon 上用 llama.cpp 的 llama-server 程式。
from marker.converters.pdf import PdfConverter
from marker.models import create_model_dict
from marker.output import text_from_rendered
converter = PdfConverter(artifact_dict=create_model_dict())
rendered = converter("report.pdf")
text, _, images = text_from_rendered(rendered)
open("report.md", "w", encoding="utf-8").write(text)
命令列用法:marker_single report.pdf。
授權: 程式碼是 Apache 2.0。模型權重採用修改版 AI Pubs OpenRAIL-M 授權,研究、個人使用以及募資或營收低於 500 萬美元的新創公司可以免費使用;更大規模的商業使用需要向 Datalab 取得授權。
批次轉換一個資料夾的 PDF
同樣的迴圈適用於任何一個函式庫,這裡以 PyMuPDF4LLM 為例:
from pathlib import Path
import pymupdf4llm
src, out = Path("pdfs"), Path("markdown")
out.mkdir(exist_ok=True)
for pdf in sorted(src.glob("*.pdf")):
try:
md = pymupdf4llm.to_markdown(str(pdf))
except Exception as e: # 損壞或加密的檔案不應中斷整批處理
print(f"skipped {pdf.name}: {e}")
continue
(out / f"{pdf.stem}.md").write_text(md, encoding="utf-8")
什麼時候不該用腳本
- 只有幾個檔案、只轉一次: 拖到轉換器裡比架環境快,還能逐頁對照 Markdown 檢查。
- 在共用電腦上處理敏感文件: 瀏覽器轉換器不用安裝,檔案只留在分頁裡。
- 想要 token 計數和 RAG 分塊又不想自己寫: 見 給 LLM 用的 PDF 轉 Markdown。
常見問題
哪個函式庫最快?
對文字版 PDF,MarkItDown 和 PyMuPDF4LLM 在筆電上每份文件只需不到一秒到幾秒。Docling 和 Marker 要執行神經網路模型,沒有 GPU 時慢得多。
哪個處理表格最好?
在我們測過的兩個裡,PyMuPDF4LLM 輸出了規範的 Markdown 表格,MarkItDown 大多沒有。Docling 和 Marker 都使用表格模型,常被推薦用於複雜表格,建議用你自己的文件試試。
能用在商業產品裡嗎?
MarkItDown 和 Docling 是 MIT 授權,可以。PyMuPDF 需要遵守 AGPL 或購買商業授權。Marker 的程式碼是 Apache 2.0,但模型權重對商業使用有營收限制。
有 JavaScript 的方案嗎?
pdftomd 本身就是在瀏覽器裡用 JavaScript 和 WebAssembly 執行的。它的文字引擎是開源的 @firecrawl/pdf-inspector-wasm 套件,OCR 是執行在 ONNX Runtime Web 上的 PaddleOCR。各元件見開源授權頁面。