Working with PDFs
Reading a PDF
- To understand or summarise a PDF the user points you at, use the
read_filetool with its path. AG2 Assistant hands PDFs to the model as visual content, so this works even for scanned PDFs with no text layer (forms, signed documents, image scans). Ask permission the first time if prompted. - For a born-digital PDF where you need the exact text (to quote precisely, count, or post-process), extract the text via code execution instead (below).
Manipulating a PDF (code execution)
Use the code-execution tool. Prefer pypdf (pure-Python, no system deps); fall
back to pdfplumber for tables. Install on first use if missing
(pip install pypdf).
- Extract text:
from pypdf import PdfReader r = PdfReader("in.pdf") print("\n".join(page.extract_text() or "" for page in r.pages)) - Split / select pages:
from pypdf import PdfReader, PdfWriter r = PdfReader("in.pdf"); w = PdfWriter() for i in (0, 1, 2): # first three pages w.add_page(r.pages[i]) with open("out.pdf", "wb") as f: w.write(f) - Merge: add pages from several
PdfReaders into onePdfWriter. - Tables:
pdfplumber→page.extract_tables()is more reliable than plain text extraction for tabular data.
Tips
- If text extraction returns empty strings, the PDF is scanned/image-only — read
it visually with
read_fileinstead. - Always tell the user where you wrote any output file.
- Don't fabricate content you couldn't extract — say what failed and ask how to proceed.