Zero-dependency PDF text extraction in pure Python. No Poppler, no PyPDF2, no MuPDF — just the standard library. It reads a PDF the way the spec describes it: xref tables, indirect objects, stream filters, font encodings, and content-stream operators.
pip install .
pdftext report.pdf
pdftext report.pdf -o report.txt --pages 1,3-5from pdftext import extract_text
print(extract_text("report.pdf"))
print(extract_text("report.pdf", pages=[2]))- Document structure — classic
xreftables,/XRefstreams, and a full-file scan fallback for PDFs with a damaged xref section. - Stream filters — FlateDecode (with PNG/TIFF predictors), ASCIIHexDecode, ASCII85Decode, RunLengthDecode, LZWDecode (with EarlyChange). Image-only filters (DCT, JPX, JBIG2) raise a clear error instead of silently producing garbage.
- Fonts — ToUnicode CMaps (
bfchar/bfrange, 1- and 2-byte codes), simple-font encodings (WinAnsi, MacRoman, Standard, PDFDoc) with/Differences, resolved through Adobe glyph names — including derived accented letters (Aacute→Á) anduniXXXX/uXXXXforms. - Content streams —
Tj,TJ(with a word-gap heuristic for large negative kerning),',", line breaks fromTd/TD/Tm/T*, multi-page documents and page ranges.
- Bytes stay bytes until the font is known. Literal strings are kept
raw through the lexer; decoding happens per font, so a ToUnicode CMap,
a
/Differencesarray, or a WinAnsi fallback each get the same input. - Best effort, never silent. A damaged xref falls back to a scan; a content stream with unparseable binary (inline images) keeps the text collected so far; an unsupported filter raises instead of guessing.
- No dependencies. Everything is
zlib,re, andunicodedata, which keeps the install trivial and the attack surface small.
- Object streams (
/ObjStm) are not supported; such files raise a clear error naming the object. - Text is extracted in content-stream order, not visual reading order; multi-column layouts may interleave lines.
- Encrypted PDFs are not supported.
python -m pytest60 tests, covering the lexer, every filter (including an LZW round-trip through a test-side encoder), CMaps, xref tables/streams, the broken-xref fallback, extraction, and the CLI.
MIT