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.

Python
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))
Output
{'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:

Python
print(by_zone("extracts/shifted-layout.pdf"))
Output
{'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.

Python
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))
Output
{'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

QuestionTemplateLanguage model
Same input, same outputAlwaysNot guaranteed
Cost per documentAlmost nothingPaid per page, every time
SpeedMillisecondsSeconds
Can explain a resultYes: this label, this positionNot really
A layout it has not seenFailsUsually copes
SetupPer kind of documentA 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.

Questions

What is zonal OCR?

Reading only fixed rectangles of a scanned page, one per field, instead of the whole page. It is template-based extraction applied to scans.

What is template-based data extraction?

A set of rules for one kind of document that says where each value is: in this rectangle, or to the right of this label. The same rules are applied to every document of that kind.

Why do extraction templates break?

Because a layout shifts: an extra line or row moves everything below it, and rules tied to fixed positions then read the wrong text. Tie each value to its label instead, and return nothing when the label is missing.

Are templates better than AI for document extraction?

For the same kinds of document in volume, yes: they give the same answer every time, cost almost nothing per page and can show where a value came from. For layouts seen once or twice, a language model is more practical.

Try it on your own documents

A free plan every month, no credit card. See the PDF data extraction API.

Start free