Cookbook
Cookbook
The guides on this site explain one technique each. The recipes put them together into complete programs for jobs people actually do, and every one of them proves its output: against the data the samples were made from, or against totals printed in the document itself.
What every recipe has in common
It runs as shown. Each recipe’s code is executed before the site is built, and the output under it is what that run printed, with the Python and pdfplumber versions it ran on. If a recipe stops working with a new release, the build fails before you see it.
It checks itself. Extraction code that runs without errors can still be wrong. The recipes compare their results with the source data of the sample files, which is published next to them, and with checks you can use on your own documents: quantities times prices, line items against subtotals, a statement’s running balance.
It uses files you can download. All sample PDFs are fictional, made for this site and released under CC0. The broken files are there to test your error handling.
Building blocks
Most extraction jobs combine a few techniques, each covered in depth elsewhere:
| You need to | Technique | Guide |
|---|---|---|
| Read a value next to a label | search(), then crop() to the right or below |
Search text |
| Read a fixed region | crop() with coordinates measured once |
Crop and coordinates |
| Get a ruled table | extract_table() with defaults |
Extract tables |
| Get a table without column lines | Column positions from the header words | Borderless tables |
| Join a table across pages | Same columns, drop repeated headers, fold split rows | Multi-page tables |
| Find headings or bold labels | Font names and sizes on each character | Words, chars and fonts |
| Read a scan | OCR first, then the same code | Scanned PDFs |
| Turn numbers into numbers | Remove separators, handle negatives, use Decimal for money |
Export tables |
| Survive bad files | Check first bytes, catch per page, flag empty pages | Damaged PDFs |
| Process thousands of pages | page.close(), worker processes |
Speed and memory |