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

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

可直接粘贴的消息

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

可以这样问 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" (not file_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_file returns 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

  • pdftotext must be installed (part of poppler-utils on Debian/Ubuntu, poppler on 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能