reliable-pdf-extraction
Use shell commands or Python libraries to extract PDF text when read_file PDF handler fails
- License 许可
- MIT
- 所属仓库
- HKUDS/AgentPilot
- 最近更新
- 2026-11-12
- Stars
- ★ 8.0k
- Forks
- 944
01 前言 Front Matter
- name
- reliable-pdf-extraction
- description
- Use shell commands or Python libraries to extract PDF text when read_file PDF handler fails
02 资源文件 1
- 其他.skill_id37 B
03 SKILL.md 正文
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
-
Attempt
read_filewithfiletype='pdf'first (in case it works) -
Check output - If you receive:
- Binary/garbage data
- Error messages
- Empty or truncated content
- Image data instead of text
-
Fall back to one of the extraction methods above:
- Use
pdftotextviarun_shellfor quick text extraction - Use
pdfplumberviaexecute_code_sandboxfor structured data/tables - Use
PyMuPDFfor complex layouts or when you need more control
- Use
-
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
04 同仓库更多 Skills
agentpilot
Set up AgentPilot and use its single entry to prepare missing inputs and run configured RSI experiments. Use when the user requests AgentPilot installation or work with AgentPilot.
remember
Review auto-memory entries and propose promotions to OPENSPACE.md, OPENSPACE.local.md, or retained auto-memory topics; detect duplicates, stale facts, conflicts, and ambiguous entries before making changes.
skill-discovery
Search reusable OpenSpace skills locally, and browse cloud packages step by step when a cloud skill may help. Reusing proven skills saves tokens, improves reliability, and extends your capabilities beyond built-in tools.
delegate-task
Delegate tasks to OpenSpace — a full-stack autonomous worker for coding, DevOps, web research, and desktop automation, backed by an extensive MCP tool and skill library. Skills auto-improve through use, reducing token consumption over time. A cloud community lets agents share and collectively evolve reusable skills.
write-special-chars-ts-check
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, plus a reliable TypeScript type-checking step that filters output to only the newly-written files to avoid being misled by pre-existing errors.
write-special-chars-readback
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, with mandatory post-write verification via read_file and cleanup of /tmp helper scripts.