Split a PDF packet into separate documents and classify each one¶
One scanned PDF. Six documents inside. No bookmarks. Acrobat and the online splitters cut by page count, file size or bookmarks. A scanned loan file has none of those: the application is 4 pages in one packet and 7 in the next, and two bank statements sit back to back.
This guide shows two ways to do it in Python: rules you write, and a model that reads the pages. Both end with one
PDF per document and a label on each.
Why the usual tools do not work on packets¶
| Method | Works when | Fails when |
|---|---|---|
| Split every N pages | every document has the same length | lengths vary, which they do |
| Split by bookmarks | the PDF was exported from software | it was scanned: no bookmarks |
| Split on a blank or barcode separator page | you control the scanning | the packet comes from a customer |
| Split on a keyword on the first page | each type has a fixed title and a text layer | scans without text, or two statements in a row |
So a real splitter has to read each page and decide two things: what kind of document this page belongs to, and whether it starts a new one.
Way 1: rules, no model (text PDFs only)¶
If your packets have a text layer and each document type starts with a known title, twenty lines are enough.
# pip install pypdf
from pypdf import PdfReader, PdfWriter
STARTS = { # text that appears on the first page of each type
"application": "uniform residential loan application",
"bank_statement": "statement period",
"tax_return": "form 1040",
}
reader = PdfReader("packet.pdf")
parts = [] # [label, [page indexes]]
for i, page in enumerate(reader.pages):
text = (page.extract_text() or "").lower()
label = next((k for k, v in STARTS.items() if v in text), None)
if label or not parts:
parts.append([label or "unknown", [i]])
else:
parts[-1][1].append(i)
for n, (label, pages) in enumerate(parts, 1):
out = PdfWriter()
for i in pages:
out.add_page(reader.pages[i])
out.write(f"{n:02d}_{label}.pdf")
Where it breaks: a scan has no text, so extract_text() returns nothing and the whole packet becomes one
"unknown" file. A statement that repeats "statement period" on every page is cut into single pages. And every new
document type is a new rule.
Way 2: let a model read the pages¶
ReadAnyDoc (open source, Apache-2.0) has a split endpoint: you describe the document types in plain words, it reads every page, scanned or not, and returns the pages of each section with a confidence. Split runs on the server, which you start locally:
# pip install requests pypdf
import requests
from pypdf import PdfReader, PdfWriter
API = "http://localhost:8000"
file_id = requests.post(f"{API}/upload", files={"file": open("packet.pdf", "rb")}).json()["file_id"]
result = requests.post(f"{API}/split", json={
"input": file_id,
"split_description": [
{"name": "Application", "description": "The signed loan application form"},
{"name": "Bank statement", "description": "Monthly bank statements",
"partition_key": "account number and statement month"},
{"name": "Tax return", "description": "Federal tax returns", "partition_key": "tax year"},
{"name": "ID", "description": "Driver's licence or passport"},
],
}).json()["result"]
reader = PdfReader("packet.pdf")
review = []
for section in result["splits"]:
# one file per separate document when the section has several (two statements, two tax years)
pieces = section.get("partitions") or [section]
for piece in pieces:
if not piece["pages"]:
continue
out = PdfWriter()
for p in piece["pages"]: # page numbers start at 1
out.add_page(reader.pages[p - 1])
name = section["name"] if piece is section else f'{section["name"]}_{piece["name"]}'
name = name.replace("/", "-").replace(" ", "_")
out.write(f"{name}.pdf")
if piece.get("conf") == "low":
review.append(name)
print("check by hand:", review)
partition_key is what separates two documents of the same type: one file per account and month, one per tax
year. Sections with conf: "low" are the ones to put in front of a person; a boundary that was moved by a rule
rather than by what is on the page is always reported low. Split reference
Label a single document¶
If the documents already arrive one per file and you only need the type, use classify. Each category has criteria,
and with "model": "accurate" the quoted evidence is checked against the page text in the same call.
label = requests.post(f"{API}/classify", json={
"input": file_id,
"classification_schema": [
{"category": "Invoice", "criteria": ["has an invoice number", "has a total due"]},
{"category": "Receipt", "criteria": ["shows a payment already made"]},
{"category": "Contract", "criteria": ["has parties and signatures"]},
],
"model": "accurate",
}).json()["result"]["category"]
Then pull the fields out¶
Once each document is its own file, extraction is a schema per type: bank statements · invoices · tax forms.
What it costs and where it is weak¶
- Cost: you pay your model provider for reading the pages. Parsing measures about $0.003-0.005 a page with the default model (benchmarks); splitting adds model calls on top of that.
- Speed: minutes for a long packet, not seconds. It is a background job, not something to run while a user
waits; use
/split_asyncand a webhook. - No public score: there is no public benchmark for packet splitting, and we have not published one. Test it on twenty of your own packets and count wrong boundaries before you trust it.
- Hosted alternatives:
Reducto, Extend and the cloud document services sell splitting as a hosted API. If you use Reducto's SDK already, the same
client.split.run(...)call works against this server.
Questions¶
How do I split a scanned PDF into separate documents automatically? Something has to read each page, because a scan has no bookmarks or text. Either run OCR and write rules on the text, or give the pages to a vision model with a description of each document type and ask for page ranges.
Can Adobe Acrobat split a packet by document type? No. It splits by number of pages, file size or top-level bookmarks. It does not look at what is on the page.
How do I separate two bank statements that follow each other?
Split on something that identifies each one: the account number and the statement month. In the code above that is
the partition_key.
How do I know a split is wrong? Check that every page landed in exactly one document, that no document is implausibly long or short for its type, and review every section reported with low confidence.