跳到主要内容

OCR 与文档处理

从 PDF 和扫描文档中提取文本。对于远程 URL 使用 web_extract,对于本地基于文本的 PDF 使用 pymupdf,对于 OCR/扫描文档使用 marker-pdf。对于 DOCX 文件使用 python-docx,对于 PPTX 文件请参阅 powerpoint 技能。

技能元数据

来源捆绑(默认安装)
路径skills/productivity/ocr-and-documents
版本2.3.0
作者Hermes Agent
许可证MIT
标签PDF, Documents, Research, Arxiv, Text-Extraction, OCR
相关技能powerpoint

参考:完整 SKILL.md

信息

以下是 Hermes 在触发此技能时加载的完整技能定义。这是技能激活时代理看到的指令。

PDF 与文档提取

对于 DOCX:使用 python-docx(解析实际文档结构,远优于 OCR)。 对于 PPTX:请参阅 powerpoint 技能(使用 python-pptx,完全支持幻灯片/备注)。 本技能涵盖 PDF 和扫描文档

步骤 1:是否有远程 URL?

如果文档有 URL,始终首先尝试 web_extract

web_extract(urls=["https://arxiv.org/pdf/2402.03300"])
web_extract(urls=["https://example.com/report.pdf"])

这通过 Firecrawl 处理 PDF 到 Markdown 的转换,无需本地依赖项。

仅在以下情况下使用本地提取:文件是本地的、web_extract 失败或需要批量处理。

步骤 2:选择本地提取器

功能pymupdf (~25MB)marker-pdf (~3-5GB)
基于文本的 PDF
扫描版 PDF (OCR)✅ (90+ 种语言)
表格✅ (基础)✅ (高精度)
公式 / LaTeX
代码块
表单
页眉/页脚移除
阅读顺序检测
图片提取✅ (嵌入式)✅ (带上下文)
图片转文本 (OCR)
EPUB
Markdown 输出✅ (通过 pymupdf4llm)✅ (原生,更高质量)
安装大小~25MB~3-5GB (PyTorch + 模型)
速度即时~1-14秒/页 (CPU), ~0.2秒/页 (GPU)

决策:除非你需要 OCR、公式、表单或复杂布局分析,否则使用 pymupdf。

如果用户需要 marker 的功能但系统缺乏约 5GB 的可用磁盘空间:

“此文档需要 OCR/高级提取(marker-pdf),这需要约 5GB 空间用于 PyTorch 和模型。您的系统有 [X]GB 可用空间。选项:释放空间、提供 URL 以便我使用 web_extract,或者我可以尝试 pymupdf,它适用于基于文本的 PDF,但不适用于扫描文档或公式。”


pymupdf(轻量级)

pip install pymupdf pymupdf4llm

通过辅助脚本

python scripts/extract_pymupdf.py document.pdf              # Plain text
python scripts/extract_pymupdf.py document.pdf --markdown # Markdown
python scripts/extract_pymupdf.py document.pdf --tables # Tables
python scripts/extract_pymupdf.py document.pdf --images out/ # Extract images
python scripts/extract_pymupdf.py document.pdf --metadata # Title, author, pages
python scripts/extract_pymupdf.py document.pdf --pages 0-4 # Specific pages

内联

python3 -c "
import pymupdf
doc = pymupdf.open('document.pdf')
for page in doc:
print(page.get_text())
"

marker-pdf(高质量 OCR)

# Check disk space first
python scripts/extract_marker.py --check

pip install marker-pdf

通过辅助脚本

python scripts/extract_marker.py document.pdf                # Markdown
python scripts/extract_marker.py document.pdf --json # JSON with metadata
python scripts/extract_marker.py document.pdf --output_dir out/ # Save images
python scripts/extract_marker.py scanned.pdf # Scanned PDF (OCR)
python scripts/extract_marker.py document.pdf --use_llm # LLM-boosted accuracy

CLI(随 marker-pdf 安装):

marker_single document.pdf --output_dir ./output
marker /path/to/folder --workers 4 # Batch

Arxiv 论文

# Abstract only (fast)
web_extract(urls=["https://arxiv.org/abs/2402.03300"])

# Full paper
web_extract(urls=["https://arxiv.org/pdf/2402.03300"])

# Search
web_search(query="arxiv GRPO reinforcement learning 2026")

pymupdf 原生支持这些功能 — 使用 execute_code 或内联 Python:

# Split: extract pages 1-5 to a new PDF
import pymupdf
doc = pymupdf.open("report.pdf")
new = pymupdf.open()
for i in range(5):
new.insert_pdf(doc, from_page=i, to_page=i)
new.save("pages_1-5.pdf")
# Merge multiple PDFs
import pymupdf
result = pymupdf.open()
for path in ["a.pdf", "b.pdf", "c.pdf"]:
result.insert_pdf(pymupdf.open(path))
result.save("merged.pdf")
# Search for text across all pages
import pymupdf
doc = pymupdf.open("report.pdf")
for i, page in enumerate(doc):
results = page.search_for("revenue")
if results:
print(f"Page {i+1}: {len(results)} match(es)")
print(page.get_text("text"))

无需额外依赖 — pymupdf 在一个包中涵盖了拆分、合并、搜索和文本提取功能。


注意事项

  • 对于 URL,web_extract 始终是首选
  • pymupdf 是安全的默认选择 — 即时响应,无需模型,随处可用
  • marker-pdf 用于 OCR、扫描文档、公式、复杂布局 — 仅在需要时安装
  • 两个辅助脚本都接受 --help 以获取完整用法
  • marker-pdf 在首次使用时会下载约 2.5GB 的模型到 ~/.cache/huggingface/
  • 对于 Word 文档:pip install python-docx(优于 OCR — 解析实际结构)
  • 对于 PowerPoint:请参阅 powerpoint 技能(使用 python-pptx)