跳到正文
Skillpedia
PDF、Word 与幻灯片7.8k · #12968

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/reliable-pdf-extraction/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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

  1. Attempt read_file with filetype='pdf' first (in case it works)

  2. Check output - If you receive:

    • Binary/garbage data
    • Error messages
    • Empty or truncated content
    • Image data instead of text
  3. Fall back to one of the extraction methods above:

    • Use pdftotext via run_shell for quick text extraction
    • Use pdfplumber via execute_code_sandbox for structured data/tables
    • Use PyMuPDF for complex layouts or when you need more control
  4. 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能