reliable-pdf-extraction
它能做什么
这项技能引导 AI 智能体在内置读取器输出质量差时,使用 pdftotext、pdfinfo 或 Python 库读取 PDF。它适合通过 AI 处理 PDF 文档的人。
适用场景
- ✓当标准 PDF 读取器返回二进制数据、错误或空白文字的时候。
- ✓当你需要从 PDF 中获取文字,用于结构化数据提取的时候。
- ✓当你想在读取文字之前先检查 PDF 的详细信息的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/reliable-pdf-extraction/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请阅读我附上的报告 PDF,并给我主要要点。普通 PDF 读取器一直报错。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会先尝试普通读取器,并检查输出。如果结果乱码或为空,它会通过 shell 运行 pdftotext 或 pdfinfo,或使用 Python 库,然后读取提取出的文字。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Reliable PDF Text Extraction
Problem
The read_file tool with filetype='pdf' often returns binary image data, errors, or unusable output when attempting to extract text from PDF documents. This makes it unreliable for structured data extraction tasks.
Solution
Use run_shell with command-line tools (pdftotext, pdfinfo) or execute_code_sandbox with Python libraries (PyMuPDF, pdfplumber) to extract PDF text content reliably.
Methods
Method 1: pdftotext (Recommended for simple extraction)
# Extract all text to stdout
pdftotext input.pdf -
# Or extract to file
pdftotext input.pdf output.txt
cat output.txt
Method 2: pdfinfo (For metadata)
pdfinfo input.pdf
Method 3: Python with PyMuPDF (fitz)
import fitz # PyMuPDF
doc = fitz.open("input.pdf")
text = ""
for page in doc:
text += page.get_text()
print(text)
doc.close()
Method 4: Python with pdfplumber (Better for tables/structured data)
import pdfplumber
with pdfplumber.open("input.pdf") as pdf:
for page in pdf.pages:
text = page.extract_text()
print(text)
# For tables:
# tables = page.extract_tables()
Workflow
Attempt
read_filewithfiletype='pdf'first (in case it works)Check output - If you receive:
- Binary/garbage data
- Error messages
- Empty or truncated content
- Image data instead of text
Fall back to one of the extraction methods above:
- Use
pdftotextviarun_shellfor quick text extraction - Use
pdfplumberviaexecute_code_sandboxfor structured data/tables - Use
PyMuPDFfor complex layouts or when you need more control
- Use
Process the extracted text for your task
Example Usage
# Via run_shell
result = run_shell(command="pdftotext document.pdf -")
pdf_text = result.stdout
# Via execute_code_sandbox
code = """
import pdfplumber
with pdfplumber.open("/path/to/document.pdf") as pdf:
for page in pdf.pages:
print(page.extract_text())
"""
result = execute_code_sandbox(code=code)
pdf_text = result.stdout
Tips
- pdftotext is fastest and most reliable for plain text extraction
- pdfplumber excels at extracting tables and preserving layout
- PyMuPDF offers the most control for complex PDF structures
- Always check if the PDF is scanned/image-based (may need OCR tools like
tesseract) - Some PDFs have copy protection that may prevent text extraction
常见问题
pdfinfo 显示什么?
它显示页数和页面大小等详细信息,有助于在提取前了解文件。
智能体总是先尝试普通读取器吗?
是的。这项技能要求先尝试普通读取器,只有输出看起来不对时才改用其他方法。
文中提到了哪些 Python 库?
这项技能提到 PyMuPDF 用于一般文字,pdfplumber 用于表格。
详情
作者的原始描述
Use shell commands or Python libraries to extract PDF text when read_file PDF handler fails
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12968
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。