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

pdf-text-extraction

它能做什么

这项技能列出了内置处理程序不可靠时,提取 PDF 文字的多种方法。它适合需要可读 PDF 内容用于分析或摘要的人。

适用场景

  • ✓当读取 PDF 返回图片数据、错误或不完整内容的时候。
  • ✓当我需要 PDF 的页数或元数据的时候。
  • ✓当我需要 PDF 中的表格数据,而简单方法不够用的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-text-extraction/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

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

可以这样问 AI

“内置 PDF 读取器无法读取我的报告。请提取其中的文字,并告诉我它有多少页。”

每周 5 个新技能,1 分钟读完。

工作原理

AI 会从 pdftotext(提取文字)、pdfinfo(获取元数据)、PyMuPDF(Python 途径)或 pdfplumber(提取表格)中选择,并在 shell 中运行所选命令。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

PDF Text Extraction (Fallback Method)

When to Use This Skill

Use this skill when read_file with filetype='pdf':

  • Returns binary image data instead of text
  • Produces errors or incomplete content
  • Fails to extract structured data reliably

The built-in PDF handler is unreliable for structured text extraction. Shell-based tools provide more robust alternatives.

Available Methods

Method 1: pdftotext (Recommended)

# Extract text from PDF to stdout
pdftotext /path/to/file.pdf -

# Extract text to a file
pdftotext /path/to/file.pdf output.txt

# Preserve layout (maintains spacing/structure)
pdftotext -layout /path/to/file.pdf output.txt

Usage in agent:

run_shell command="pdftotext -layout /path/to/document.pdf -"

Method 2: pdfinfo (Metadata)

# Get PDF metadata (pages, author, creation date, etc.)
pdfinfo /path/to/file.pdf

Usage in agent:

run_shell command="pdfinfo /path/to/document.pdf"

Method 3: Python with PyMuPDF (fitz)

import fitz  # PyMuPDF

doc = fitz.open("/path/to/file.pdf")
text = ""
for page in doc:
    text += page.get_text()
doc.close()
print(text)

Usage in agent:

run_shell command="python3 -c \"import fitz; doc=fitz.open('file.pdf'); print(''.join(p.get_text() for p in doc))\""

Method 4: Python with pdfplumber (Tables)

import pdfplumber

with pdfplumber.open("/path/to/file.pdf") as pdf:
    for page in pdf.pages:
        text = page.extract_text()
        tables = page.extract_tables()  # For tabular data

Usage in agent:

run_shell command="python3 -c \"import pdfplumber; pdf=pdfplumber.open('file.pdf'); print(''.join(p.extract_text() or '' for p in pdf.pages))\""

Workflow

  1. Try pdftotext first - Fastest and most reliable for plain text

    run_shell command="pdftotext -layout /path/to/file.pdf -"
    
  2. If pdftotext unavailable, check for Python libraries

    run_shell command="python3 -c \"import fitz; print('PyMuPDF available')\""
    
  3. For table/structured data, use pdfplumber

    run_shell command="python3 -c \"import pdfplumber; ...\""
    
  4. Verify extraction succeeded - Check output contains readable text, not binary data

Installation Notes

If tools are not available:

# Ubuntu/Debian
apt-get install poppler-utils  # pdftotext, pdfinfo

# Install Python libraries
pip install pymupdf pdfplumber

Anti-Patterns to Avoid

  • Do NOT rely solely on read_file with filetype='pdf' for critical text extraction
  • Do NOT assume PDF text is in any particular order - verify extracted content
  • Do NOT use image-based extraction unless the PDF is scanned (use OCR instead)

Example: Complete Extraction Pattern

# Step 1: Try pdftotext
RESULT=$(pdftotext -layout /path/to/form.pdf - 2>/dev/null)

# Step 2: Verify we got text, not error
if [ -z "$RESULT" ]; then
    # Fallback to Python
    RESULT=$(python3 -c "import fitz; doc=fitz.open('/path/to/form.pdf'); print(''.join(p.get_text() for p in doc))")
fi

# Step 3: Use extracted text
echo "$RESULT"
常见问题

pdfinfo 会显示什么?

它会显示页数、标题、作者和创建日期等元数据。

什么时候 pdfplumber 有用?

指南建议用它从 PDF 中提取表格。

内置处理程序总是不可靠吗?

指南说,它在结构化文字提取方面不可靠,因此优先使用 shell 工具。

详情

作者的原始描述

Extract text from PDFs using shell tools when read_file fails

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#12935
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能