PDF Processing Skill
Overview
This skill handles all PDF operations: text extraction, table parsing, form filling, merging, splitting, and metadata extraction.
Step-by-Step Guidance
1. Identify the Operation
Determine what the user needs:
- Text extraction: Read content from a PDF into plain text or markdown
- Table extraction: Pull structured tabular data into CSV/JSON
- Form filling: Write values into fillable PDF form fields
- Merge: Combine multiple PDFs into one document
- Split: Divide a PDF into separate pages or sections
- Metadata: Extract author, creation date, page count, etc.
2. Choose the Right Library
For Python environments:
pypdf(formerly PyPDF2): text extraction, merging, splitting, metadata - pure Python, no system depspdfplumber: best for tables and layout-aware extractionpymupdf(fitz): fastest, handles scanned PDFs with OCR via Tesseractreportlab: generate new PDFs or fill forms programmaticallypdfrw: low-level PDF manipulation, ideal for form filling
3. Text Extraction
import pypdf
reader = pypdf.PdfReader("document.pdf")
text = "\n\n".join(page.extract_text() for page in reader.pages)
print(text)
For layout-preserving extraction:
import pdfplumber
with pdfplumber.open("document.pdf") as pdf:
for page in pdf.pages:
print(page.extract_text(layout=True))
4. Table Extraction
import pdfplumber
with pdfplumber.open("document.pdf") as pdf:
for page in pdf.pages:
tables = page.extract_tables()
for table in tables:
for row in table:
print(row)
5. Merging PDFs
import pypdf
merger = pypdf.PdfMerger()
for path in ["file1.pdf", "file2.pdf", "file3.pdf"]:
merger.append(path)
merger.write("merged.pdf")
merger.close()
6. Splitting a PDF
import pypdf
reader = pypdf.PdfReader("document.pdf")
for i, page in enumerate(reader.pages):
writer = pypdf.PdfWriter()
writer.add_page(page)
with open(f"page_{i+1}.pdf", "wb") as f:
writer.write(f)
7. Form Filling
import pypdf
reader = pypdf.PdfReader("form.pdf")
writer = pypdf.PdfWriter()
writer.append(reader)
writer.update_page_form_field_values(
writer.pages[0],
{"field_name": "value", "another_field": "another_value"}
)
with open("filled_form.pdf", "wb") as f:
writer.write(f)
Edge Cases
- Scanned PDFs (image-only): use
pymupdf+ Tesseract OCR - warn the user if no text is found - Password-protected PDFs: call
reader.decrypt("password")before reading - Large PDFs (>100 pages): process page by page to avoid memory issues
- Right-to-left text (Arabic, Hebrew):
pdfplumberhandles RTL better thanpypdf
Output Format
- Plain text extraction → return as a markdown code block
- Table extraction → return as CSV or a markdown table
- File operations (merge/split) → confirm output file path and page count