Skip to content

PDF Oxide vs pypdf

PDF Oxide は pypdf より 15 倍速く、パス率も高く、レンダリング、Markdown/HTML 出力、OCR、フォーム操作まで標準装備しています。pypdf で複数のパッケージを組み合わせる必要がある処理を、PDF Oxide は単一のライブラリでまかなえます。

なぜ pypdf の代わりに PDF Oxide か

速度。 pypdf はピュア Python です。PDF Oxide は PyO3 でコンパイルした Rust コアが Python プロセス内で直接動きます。テキスト抽出の平均値は 0.8 ms 対 12.1 ms で、15 倍の差があります。

信頼性。 PDF Oxide は 3,830 件のテスト PDF をすべて通過 (100%)。pypdf は 98.4% で、有効な PDF 61 件に失敗します。

機能範囲。 pypdf は PDF の編集 (結合、分割、回転、暗号化) に強みがあるライブラリです。テキスト抽出、レンダリング、Markdown 出力、フォーム生成には別パッケージが必要です。PDF Oxide はこれらを 1 回のインストールでカバーします。

簡易比較

PDF Oxide pypdf
平均抽出時間 0.8 ms 12.1 ms
パス率 (3,830 PDF) 100% 98.4%
ライセンス MIT BSD-3
言語 Rust + PyO3 ピュア Python
テキスト抽出 あり あり
文字位置 あり 部分的
画像抽出 あり あり
Markdown 出力 あり なし
HTML 出力 あり なし
PDF 生成 あり (Markdown/HTML/画像) 限定的 (結合のみ)
フォームフィールド 読み取り + 書き込み 読み取り + 書き込み
暗号化 読み取り + 書き込み 読み取り + 書き込み
レンダリング あり なし
OCR 組み込み なし
検索 正規表現 + 空間 なし
インストールサイズ 約 5 MB 約 1 MB

コードの並列比較

テキスト抽出

PDF Oxide:

from pdf_oxide import PdfDocument

doc = PdfDocument("report.pdf")
text = doc.extract_text(0)
print(text)

pypdf:

from pypdf import PdfReader

reader = PdfReader("report.pdf")
text = reader.pages[0].extract_text()
print(text)

全ページの抽出

PDF Oxide:

from pdf_oxide import PdfDocument

doc = PdfDocument("book.pdf")
for i in range(doc.page_count()):
    text = doc.extract_text(i)
    print(f"--- Page {i + 1} ---")
    print(text)

pypdf:

from pypdf import PdfReader

reader = PdfReader("book.pdf")
for page in reader.pages:
    text = page.extract_text()
    print(text)

画像抽出

PDF Oxide:

from pdf_oxide import PdfDocument

doc = PdfDocument("report.pdf")
images = doc.extract_image_bytes(0)
for i, img in enumerate(images):
    with open(f"image_{i}.{img['format']}", "wb") as f:
        f.write(img["data"])

pypdf:

from pypdf import PdfReader

reader = PdfReader("report.pdf")
page = reader.pages[0]
for i, image in enumerate(page.images):
    with open(f"image_{i}.{image.name.split('.')[-1]}", "wb") as f:
        f.write(image.data)

暗号化 PDF

PDF Oxide:

from pdf_oxide import PdfDocument

doc = PdfDocument("encrypted.pdf", password="secret")
text = doc.extract_text(0)

pypdf:

from pypdf import PdfReader

reader = PdfReader("encrypted.pdf")
reader.decrypt("secret")
text = reader.pages[0].extract_text()

Markdown への変換

PDF Oxide (組み込み):

from pdf_oxide import PdfDocument

doc = PdfDocument("paper.pdf")
md = doc.to_markdown(0, detect_headings=True)
print(md)

pypdf:

# pypdf には Markdown 変換がない。
# 別途ツールチェーンを組む必要がある。

ベンチマークの詳細

指標 PDF Oxide pypdf
平均抽出時間 0.8 ms 12.1 ms
p99 抽出時間 9 ms 97 ms
パス率 (有効 PDF) 100% (3,823/3,823) 98.4% (3,762/3,823)

pypdf はピュア Python 実装なので、すべての処理がインタプリタ上で走ります。一方 PDF Oxide の Rust コアはパース、フォントデコード、テキスト組み立てをネイティブで処理し、Python 側に渡されるのは最終結果だけです。

コーパスの詳細は ベンチマーク方法論の全文 を参照してください。

機能差

pypdf は結合、分割、回転、暗号化といった PDF 編集に強みがあります。一方で次の機能は欠けています:

機能 PDF Oxide pypdf
Markdown 変換 doc.to_markdown(0) 非対応
HTML 変換 doc.to_html(0) 非対応
内容から PDF 生成 Pdf.from_markdown()Pdf.from_html() 非対応
画像へのレンダリング あり 非対応
スキャン PDF の OCR 組み込み PaddleOCR 非対応
テキスト検索 doc.search("query") 非対応
文字単位のバウンディングボックス doc.extract_chars(0) 部分的
PDF/A の検証 あり 非対応

ワークフローが結合・分割・回転のみなら、pypdf の軽量なピュア Python 路線は妥当な選択です。テキスト抽出の品質、PDF 生成、変換が絡むなら PDF Oxide のほうが機能的にそろっています。

pypdf を継続利用すべきケース

  • コンパイル済み拡張モジュールを含まない、ピュア Python の依存が必要
  • ユースケースがテキスト抽出を伴わず、結合/分割/回転/暗号化に限定されている
  • レガシー統合で pypdf 固有の PDF 編集メソッドが必要

関連ページ