pdf-text-extraction-9424c5
它能做什么
这项技能说明,当常用读取器失败时,如何用 pdftotext 从 PDF 中提取可读文字。它适合处理 AI 无法直接读取的 PDF 文档的人。
适用场景
- ✓当读取 PDF 返回乱码或二进制内容的时候。
- ✓当 PDF 包含扫描图片或复杂的二进制结构的时候。
- ✓当我需要把 PDF 文字保存到文件,或用于后续步骤的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-text-extraction-9424c5/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请读取我的 document.pdf 中的文字。普通读取器输出了乱码,所以请用 pdftotext 提取。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会确认文件存在,运行 pdftotext 获取文字,并直接使用输出结果;如有需要,也可以把它写入临时文本文件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
PDF Text Extraction via pdftotext
Problem
When using read_file on PDF documents, the function may return binary image data or garbled content instead of readable text. This occurs because PDFs can contain scanned images or complex binary structures that read_file cannot properly parse as text.
Solution
Use the pdftotext command-line utility via run_shell to extract clean text content from PDF files.
Steps
1. Verify PDF file exists
import os
pdf_path = "path/to/document.pdf"
if not os.path.exists(pdf_path):
raise FileNotFoundError(f"PDF not found: {pdf_path}")
2. Extract text using pdftotext
from tools import run_shell
# Extract text to stdout
result = run_shell(command=f"pdftotext '{pdf_path}' -", timeout=60)
pdf_text = result.stdout
# Alternative: extract to a temporary file
temp_txt = "/tmp/extracted.txt"
run_shell(command=f"pdftotext '{pdf_path}' '{temp_txt}'", timeout=60)
with open(temp_txt, 'r') as f:
pdf_text = f.read()
3. Handle parameter naming carefully
When calling read_file, be aware of the parameter name:
- Use
filetype="pdf"(notfile_type) - Some tool implementations may use different parameter names
# Correct parameter usage
content = read_file(file_path="doc.pdf", filetype="pdf")
# If this returns binary/garbled data, fall back to pdftotext
Common pdftotext Options
| Option | Description |
|---|---|
- |
Output to stdout |
-layout |
Maintain original layout |
-f <n> |
Start from page n |
-l <n> |
End at page n |
-q |
Quiet mode |
Example with options:
result = run_shell(command=f"pdftotext -layout -q '{pdf_path}' -", timeout=60)
Error Handling
from tools import run_shell
def extract_pdf_text(pdf_path):
"""Extract text from PDF using pdftotext with error handling."""
import os
if not os.path.exists(pdf_path):
raise FileNotFoundError(f"PDF not found: {pdf_path}")
result = run_shell(command=f"pdftotext '{pdf_path}' -", timeout=60)
if result.returncode != 0:
raise RuntimeError(f"pdftotext failed: {result.stderr}")
return result.stdout.strip()
When to Use This Pattern
read_filereturns binary data, garbled text, or image content for a PDF- You need searchable/processable text from PDF documents
- The PDF contains text (not just scanned images - for those, consider OCR tools)
Prerequisites
pdftotextmust be installed (part ofpoppler-utilson Debian/Ubuntu,poppleron macOS via Homebrew)- Verify availability:
run_shell(command="which pdftotext")
常见问题
为什么要先检查文件是否存在?
如果 PDF 路径有误,指南会给出清晰的错误并停止。
read_file 的参数提示是什么?
指南说,调用读取工具时应使用 filetype,而不是 file_type。
输出可以放在哪里?
可以打印到屏幕上,也可以写入文本文件。
详情
作者的原始描述
Extract text from PDF files using pdftotext when read_file returns binary data
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12932
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。