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

pdf-extract-progressive-tools

它能做什么

这项技能规定了在首选方法返回无法读取的数据时,提取 PDF 文字的逐步顺序。适合 AI 从 PDF 中只得到二进制或乱码输出的人。

适用场景

  • ✓当读取 PDF 时返回的是二进制或图像数据而非文字时。
  • ✓当我需要可靠的 PDF 文字用于摘要或分析时。
  • ✓当文件是简单工具无法读取的复杂 PDF 时。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-download-extract-fallback-enhanced-899f5b/SKILL.md

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“请帮我:我的 PDF 一直得到乱码文字。请先试最简单的方法,然后按顺序尝试其他工具,直到得到干净的文字。”

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

工作原理

AI 先尝试简单的文件读取,失败则在 shell 中运行 pdftotext,最后在沙盒中运行 PyMuPDF 的 Python 脚本,并在结束时检查文字质量。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

PDF Text Extraction with Progressive Tool Fallback

This skill provides a robust workflow for extracting text from PDF documents using a sequenced approach with agent tools, with explicit fallback mechanisms based on observed tool behavior.

Critical Insight from Execution Data

read_file often returns binary/image data for PDFs, not extracted text. When this occurs, immediately escalate to run_shell with pdftotext before attempting Python-based extraction.

Entry Point: Determine Your Starting Point

Before beginning, identify your scenario:

Scenario Start Here Skip
PDF already on local disk Step 1 (read_file attempt) Download steps
PDF at a web URL Download first, then Step 1 None
PDF content already extracted Step 4 (Quality verification) Steps 1-3

Overview

PDF extraction failures cascade when tool sequencing is unclear. This workflow ensures maximum success rate through explicit tool progression:

  1. read_file - Quick attempt, but may return binary data
  2. run_shell + pdftotext - Reliable extraction when read_file fails
  3. execute_code_sandbox + PyMuPDF - Final fallback for complex PDFs

Step-by-Step Instructions

Step 1: Attempt read_file First

Always try the simplest approach first:

Tool: read_file
Path: document.pdf

Expected outcome: Extracted text content

Critical check: Examine the returned content:

  • ✅ Text visible: Proceed to Step 4 (Quality verification)
  • ⚠️ Binary/image data detected: Immediately proceed to Step 2
  • ❌ File not found: Verify path or download first

Binary data indicators:

  • Content starts with %PDF- header without text extraction
  • Content appears as garbled characters or base64
  • Content contains PNG/JPEG markers within PDF wrapper
  • File size seems reasonable but no readable text

Step 2: Escalate to run_shell with pdftotext

When read_file returns binary data, do NOT attempt execute_code_sandbox yet. Use run_shell immediately:

Tool: run_shell
Command: pdftotext document.pdf document.txt

If pdftotext is not available:

Tool: run_shell
Command: apt-get update && apt-get install -y poppler-utils && pdftotext document.pdf document.txt

Then read the extracted text:

Tool: read_file
Path: document.txt

Expected outcome: Clean text extraction

If this fails:

  • Check if file is password-protected
  • Check if file is corrupted (run file document.pdf)
  • Proceed to Step 3

Step 3: Final Fallback to execute_code_sandbox with PyMuPDF

Only attempt this if Steps 1-2 fail:

Tool: execute_code_sandbox
Language: python
Code: |
  import fitz  # PyMuPDF
  
  try:
      doc = fitz.open("document.pdf")
      text = ""
      for page in doc:
          text += page.get_text()
      doc.close()
      
      with open("document_pymupdf.txt", "w") as f:
          f.write(text)
      
      print("SUCCESS: Extracted {} characters".format(len(text)))
  except Exception as e:
      print(f"FAILED: {e}")

Then read the result:

Tool: read_file
Path: document_pymupdf.txt

Step 4: Quality Verification

Regardless of which method succeeded, verify extraction quality:

  1. Check text length: Should be proportional to PDF pages (~500-2000 chars per page)
  2. Check readability: Text should form coherent sentences
  3. Check for truncation: Look for cut-off words or missing sections
  4. Compare methods: If multiple methods worked, compare outputs

If quality is poor:

  • Try alternative extraction tools (pdfplumber, camelot-py for tables)
  • Consider OCR for scanned documents
  • Document limitations clearly

Step 5: Graceful Degradation to Domain Knowledge

If all extraction methods fail:

  1. Document the specific failure mode for each tool attempted
  2. Extract any partial content that was successfully retrieved
  3. Supplement missing content from established domain knowledge
  4. Clearly mark which portions are from source vs. generated from knowledge
  5. Provide citations for any claimed requirements or specifications

Example degradation note:

NOTE: Source document [path/URL] was inaccessible due to [specific tool failures].
Content below combines partial extraction with established domain knowledge 
for [topic]. All claims verified against [alternative sources] where possible.

Tool Failure Log:
- read_file: Returned binary data (no text extraction)
- run_shell/pdftotext: Comman
常见问题

为什么先从简单读取开始?

指南称这是最快的尝试,不过有时只会返回二进制数据。

为什么 pdftotext 排在 Python 之前?

指南发现在受限环境中,shell 工具更可靠。

会检查文字质量吗?

会。指南包含一个步骤,用于核对提取文字的质量。

详情

作者的原始描述

Progressive tool-chain PDF extraction with explicit read_file, run_shell, and execute_code_sandbox sequencing

repo: HKUDS/AgentPilot

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能