PDF Oxide vs pypdf
PDF Oxide 比 pypdf 快 15 倍,通过率更高,渲染、Markdown/HTML 导出、OCR 与表单处理都直接内置。pypdf 需要组合多个包才能完成的事情,PDF Oxide 一个库就能搞定。
为什么考虑用 PDF Oxide 替代 pypdf
速度。 pypdf 是纯 Python 实现。PDF Oxide 使用 PyO3 编译的 Rust 核心,直接在 Python 进程里运行。平均文本抽取:0.8 ms vs 12.1 ms —— 差距 15 倍。
稳定性。 PDF Oxide 在 3830 个测试 PDF 上通过率 100 %。pypdf 是 98.4 %,在 61 个合法 PDF 上会失败。
功能覆盖。 pypdf 定位是 PDF 操作库(合并、拆分、旋转、加密)。文本抽取、渲染、Markdown 输出、表单生成都需要额外的包。PDF Oxide 一次安装就能覆盖这些能力。
快速对比
| PDF Oxide | pypdf | |
|---|---|---|
| 平均抽取时间 | 0.8 ms | 12.1 ms |
| 通过率 (3830 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% (3823/3823) | 98.4% (3762/3823) |
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") |
不具备 |
| 字符级 bounding box | doc.extract_chars(0) |
部分支持 |
| PDF/A 校验 | 支持 | 不具备 |
如果你的流程只涉及合并/拆分/旋转,pypdf 这种轻量纯 Python 方案完全合理。一旦触及文本抽取质量、PDF 生成或格式转换,PDF Oxide 在完整度上就是更合适的选择。
什么情况下仍选择 pypdf
- 需要纯 Python 依赖,拒绝任何编译扩展
- 用例严格限于合并/拆分/旋转/加密,没有文本抽取需求
- 出于遗留系统整合,依赖 pypdf 特有的 PDF 操作方法
相关页面
- 性能基准 —— 完整语料结果
- vs Python PDF 库 —— 所有 Python 库横向对比
- Python 入门 —— 安装与首次抽取