Template-based extraction: when fixed positions beat AI
Updated · Tested with Python 3.13, pdfplumber 0.11.11, PyMuPDF 2.2
In short
Template-based extraction reads each value from a known place on every document of one kind. Fixed rectangles are exact and fast but return the wrong text, with no error, when the layout shifts. Anchoring each value to its label fixes that. For documents that never change it is more repeatable and far cheaper per page than a language model.
Template-based extraction means telling the computer where a value is, once, and then reading that place on every document of the same kind. It is also called zonal extraction, or zonal OCR when the pages are scans. It is the oldest method there is, and for documents that never change it is still the most exact and the cheapest. The examples use three company extracts with the same layout and different values: harbor-lane.pdf, northgate-freight.pdf and shifted-layout.pdf.
Zones: read a fixed rectangle
Look up where the values sit on one document, write down a rectangle for each field, and read those rectangles everywhere. Coordinates are in points from the top left: left, top, right, bottom.
import pdfplumber
zones = {
"company_name": (200, 128, 560, 146),
"company_number": (200, 150, 560, 168),
"status": (200, 172, 560, 190),
}
def by_zone(path):
with pdfplumber.open(path) as pdf:
page = pdf.pages[0]
return {name: page.crop(box).extract_text() for name, box in zones.items()}
for path in ["extracts/harbor-lane.pdf", "extracts/northgate-freight.pdf"]:
print(by_zone(path)){'company_name': 'Harbor Lane Logistics Ltd', 'company_number': '51-482913-6', 'status': 'Active'}
{'company_name': 'Northgate Freight Ltd', 'company_number': '51-509871-2', 'status': 'Active'}Two different documents, the same three rectangles, the right answers. No pattern matching, no model, and the same input always gives the same output.
Where zones fail
The third file is the same kind of extract with one extra line near the top and one extra row. Everything below moved down:
print(by_zone("extracts/shifted-layout.pdf")){'company_name': 'ence 2026/8841.', 'company_number': 'Quayside Storage Ltd', 'status': 'Quayside Cold Stores Ltd'}Every value is wrong, and nothing says so. The code returned text from the right rectangles; the document simply no longer had the right things in them. This is the weakness of fixed zones: they fail without an error, and the result still looks like data.
Anchors: measure from a label
The fix is to stop using positions on the page and use positions relative to something that moves with the value: its label. Find the label, then read what is to its right.
import pymupdf
labels = {"company_name": "Company name", "company_number": "Company number", "status": "Status"}
def by_label(path):
values = {}
with pymupdf.open(path) as doc:
page = doc[0]
for name, label in labels.items():
found = page.search_for(label)
if not found:
values[name] = None
continue
box = found[0]
right_of_label = pymupdf.Rect(box.x1, box.y0 - 2, page.rect.width, box.y1 + 2)
values[name] = page.get_text("text", clip=right_of_label).strip() or None
return values
for path in ["extracts/harbor-lane.pdf", "extracts/shifted-layout.pdf"]:
print(by_label(path)){'company_name': 'Harbor Lane Logistics Ltd', 'company_number': '51-482913-6', 'status': 'Active'}
{'company_name': 'Quayside Storage Ltd', 'company_number': '51-640027-5', 'status': 'Active'}The shifted document is now read correctly. And when a label is missing, the result is None, not someone else's value. An anchored template knows when it has failed, which matters more than being right slightly more often.
When a template beats AI, and when it does not
| Question | Template | Language model |
|---|---|---|
| Same input, same output | Always | Not guaranteed |
| Cost per document | Almost nothing | Paid per page, every time |
| Speed | Milliseconds | Seconds |
| Can explain a result | Yes: this label, this position | Not really |
| A layout it has not seen | Fails | Usually copes |
| Setup | Per kind of document | A prompt |
Use a template for documents that look the same every time and arrive in volume: registry extracts, statements from one bank, invoices from one supplier, your own forms. Use a model for the long tail: documents that each look different and arrive in ones and twos. OCR, LLMs or templates compares the three in more detail.
Making a template that lasts
- Anchor to labels, never to the page, as above.
- Check every value. A company number has a known shape; a date must parse; a total must add up. A value that fails its check goes to a person.
- Return nothing, not a guess, when a label is not found.
- Keep versions. When the form changes, add a new template and keep the old one, because old documents keep arriving.
- Test against real samples every time you change it, including the awkward ones.
Scans: zonal OCR
The same idea works on scanned pages, with OCR supplying the text and its positions. Two things change: positions shift a little from scan to scan, so anchors are not optional, and you should make zones generous. Reading only a cropped field with Tesseract's single-line mode is often more accurate than reading the whole page; see how to improve OCR accuracy.
Templates without writing them
The work in a template is finding the anchors and rules for each field and proving they hold on more than one document. 100sands does that part with AI, once, for PDFs and HTML files that contain text: you upload one to five samples of a kind of document, AI suggests the fields and each sample's values, you check and correct them, and AI sets up the extraction. It goes live only when it returns every value you checked on every sample. From then on documents are read by that fixed extraction, not by a model, so the same file always gives the same answer, and a value that is not in the document comes back empty instead of guessed at. Each change is a new version, checked against all the samples again. It does not read scans, so it is not a zonal OCR tool. The PDF data extraction API page shows the call.