LlamaIndex 教程:如何程序化提取 PDF 数据,从判断文件实际内容入手
How to Extract Data From PDFs Programmatically, Starting With What the File Actually Contains
LlamaIndex 发布 PDF 数据提取教程,强调写代码前先判断文件是原生数字、扫描还是混合类型,并给出用 pypdf 分类文本层的示例。教程对比 pypdf、pdfplumber、PyMuPDF 和 Camelot 的许可、速度与表格能力,讲解双栏交错、缺 ToUnicode 映射和连字等常见坑,指出 Tesseract OCR 会丢失表格结构。
If you open the raw bytes of a bank statement, the line that prints "Total" will look something like BT /F1 10 Tf 72 712 Td (Total) Tj ET.
That is an instruction to draw five glyphs in font F1 at a point 72 units from the left edge. Somewhere else in the file, another instruction draws "$4,210.00" a few hundred units to the right. The format never says that those two strings belong together, that they sit in the same table row, or that the page has a table at all.
A PDF simply describes where ink goes. Your eye reconstructs paragraphs, columns, and cells. Extraction libraries have to reconstruct them too.

That is why most of the work in learning how to extract data from PDFs programmatically comes before you write any code. The library you choose depends on what kind of file you have, and the most common mistake is skipping straight to pip install without checking.
In this guide, we'll go in the order the decisions actually come up: what kind of file is this, what can the open-source libraries get out of it, what happens on scans, and how strings become fields. Then we'll look at collapsing all of it into one call.
Find Out Whether Your PDF Has a Text Layer Before You Choose a Library
Every PDF document you'll process falls into one of three buckets:
- Born-digital. Exported from Word, an ERP, or a reporting tool. Libraries can read it directly.
- Scanned. Each page is a picture of paper. Text extraction returns nothing, so you need OCR.
- Mixed. A digital contract with a scanned signature page, or a statement with an image-only appendix. This is the type of document that breaks pipelines built for the other two.
The proportions depend entirely on where your files come from. In one synthetic 4,911-file test corpus built on Enron data, 44% were digital, 40% were scans with an OCR layer already applied, and 16% were image-only. A FOIA release or an archive of old court filings would look very different. The only way to know your split is to measure it:
from pypdf import PdfReader
def classify(path, min_chars=50):
"""Label each page 'text', 'scan', or 'empty', then label the whole file."""
labels = []
for page in PdfReader(path).pages:
text = (page.extract_text() or "").strip()
if len(text) >= min_chars:
labels.append("text")
elif page.images:
labels.append("scan")
else:
labels.append("empty")
if all(label == "text" for label in labels):
return "born-digital", labels
if "text" not in labels:
return "scanned", labels
return "mixed", labels
Run that across a folder and you’ll get a routing table. It won't catch every failure mode, though. A scan that someone already ran through OCR passes as "text" even if the hidden text layer is garbage. If the extracted text from a "searchable" scan reads like alphabet soup, treat the file as scanned and re-extract it from the page images rather than trusting whatever OCR pass someone ran years ago.
Born-Digital PDFs: Getting Text Out Takes One Line, Getting Structure Out Doesn't
On a clean born-digital file, pulling text from a PDF really is easy. If all you need is to extract text for search or a quick summary, a single library call does it, and you can stop here. The difficulty starts when you need to know which words belong together.
Picking Between pypdf, pdfplumber, and PyMuPDF
The libraries you'll see in every tutorial do different jobs:
| Library | License | Speed | Word positions | Tables | Best for |
|---|---|---|---|---|---|
| pypdf | BSD-3 | Moderate | No | No | Plain text, zero dependencies |
| pdfplumber | MIT | Slow (~3.4 s/doc) | Yes, per word and character | extract_tables() |
Debugging layout, ruled tables |
| PyMuPDF | AGPL-3.0 or commercial | Fast (~0.34 s/doc) | Yes, words and blocks | find_tables() |
Volume work |
| Camelot | MIT | Moderate | No | Lattice and stream modes | Tables in text-based PDFs |
The speed figures come from the pdftext benchmark of 200 Common Crawl PDFs, measured per document on an Apple Silicon laptop, which puts PyMuPDF about ten times faster than pdfplumber. The license column matters too. PyMuPDF is dual-licensed under the AGPL or a commercial license, so shipping it inside a SaaS product or a distributed binary either brings you under the copyleft terms or means buying a license from Artifex.
Here's the same first page read three ways:
from pypdf import PdfReader
text = PdfReader("statement.pdf").pages[0].extract_text()
import pdfplumber
with pdfplumber.open("statement.pdf") as pdf:
words = pdf.pages[0].extract_words() # dicts with text, x0, top, x1, bottom
import pymupdf # older code imports this as fitz
doc = pymupdf.open("statement.pdf")
blocks = doc[0].get_text("blocks") # (x0, y0, x1, y1, text, block_no, type)The pypdf call gives you a string. The other two give you coordinates, and coordinates are what the rest of this guide depends on.
Three Bugs You'll Hit in the First Week
Columns get interleaved. On a two-column page, text extraction reads straight across the full width of the page. We built a two-column test file, and pdfplumber returned "Left column. The beneficial owner Right column. Officers and..." as the first line, which is half a sentence from each column glued together. The fix is to crop each column before extracting:
with pdfplumber.open("filing.pdf") as pdf:
page = pdf.pages[0]
mid = page.width / 2
left = page.crop((0, 0, mid, page.height)).extract_text()
right = page.crop((mid, 0, page.width, page.height)).extract_text()That works until the next document puts the gutter somewhere else.
(cid:42) shows up instead of letters. A font embedded without a ToUnicode map gives the extractor glyph IDs and no way to turn them back into characters. pdfminer.six (which pdfplumber is built on) prints (cid:NN) placeholders, one of the most common issues in its FAQ, and other libraries can return plausible-looking garbage. Some tools can rebuild the mapping from the embedded font's own tables, but often nothing in the file maps those glyphs back to letters. Those pages belong on the OCR path even though they technically have a text layer.
Ligatures break matching. Typeset documents often store "fi" as the single character U+FB01, so a search for "financial" misses "financial". Run unicodedata.normalize("NFKC", text) on everything you extract.
Tables: Where Born-Digital Stops Being Easy
For a table with ruling lines, both pdfplumber's extract_tables() and Camelot's lattice mode do well. On our test statement, both returned the four rows exactly:
import camelot
tables = camelot.read_pdf("statement.pdf", pages="1-end", flavor="lattice")
rows = tables[0].df # a pandas DataFrameCamelot's docs recommend lattice for bordered tables and stream for tables separated only by whitespace. Neither mode works on a scan, because both read the text layer. And neither knows that the transaction table on page 4 continues on page 5. Merged header cells, a header row repeated mid-table, and a running balance that wraps onto the next page all become code you write and maintain yourself. On a 30-page brokerage statement, that stitching code often ends up longer than the rest of the script combined. Our guide on how to extract tables from PDFs covers that problem on its own.
Most quick tutorials show Tesseract running on a PNG, which skips the step you actually need: getting from a scanned PDF file to images. That step needs poppler for rendering, plus the Tesseract binary itself, both installed at the OS level:
from pdf2image import convert_from_path
import pytesseract
pages = convert_from_path("scanned.pdf", dpi=300)
text = "\n".join(pytesseract.image_to_string(img) for img in pages)Tesseract's own guidance is to render at 300 DPI or higher, and deskewing and thresholding help at the margins. If you'd rather stay in the born-digital stack, ocrmypdf scanned.pdf searchable.pdf adds a text layer to the file and lets pdfplumber or Camelot take over.
The catch is what Tesseract gives you back. image_to_string returns one flat string. The table structure is gone. Scan a statement with separate Debits and Credits columns, and each row comes back as a single line with one number on the end, so a $3,250.00 deposit and a $1,450.00 rent payment look identical. In one test run, the rent payment read as "1 450.00" with its comma missing.
Ruling lines show up as stray = and | characters. Stamps and signatures turn into stray characters, and handwritten fields mostly vanish. Tesseract does report per-word confidence through image_to_data, but nothing downstream in this stack uses it unless you write that code too. Preprocessing makes the characters cleaner. It doesn't bring back the layout that OCR didn’t capture. For a rundown of tools that go further on scans and images, see our comparisons of OCR software and image-to-text converters.

Text Still Isn't Data Until Something Maps It to Fields
Everything so far produces strings. What the downstream system wants is a record: invoice_number, due_date, total, and a list of line items. The usual next step is regex anchored on labels:
import re
match = re.search(r"Invoice\s*(?:#|No\.?|Number)[:\s]*([A-Z0-9-]+)", text)Invoices From Fifty Vendors
That pattern works for the vendor you wrote it against. The second vendor prints "Inv. Ref", the third puts the number above its label, and the fourth splits it across two lines because the column was narrow. Each regex is a custom template, and an accounts-payable team receiving invoices from fifty suppliers ends up maintaining fifty of them. This is the workload invoice data extraction tools exist for, and our post on automating invoice processing walks through why line items are the hard part.
Bank and Brokerage Statements
Statements have a property invoices lack: they check themselves. Opening balance plus every transaction should equal the closing balance, so a dropped row on a page break shows up as a mismatch:
assert abs(opening + sum(t["amount"] for t in transactions) - closing) < 0.01Put checks like that in code no matter which extraction tool you use. Totals should sum, dates should parse, and required fields should never come back null. Bank statement OCR lives or dies on this kind of completeness.
Look at what the do-it-yourself stack has become. There's a text-layer check, a choice between three libraries, Camelot for tables, Tesseract for scans, column-cropping heuristics, and a layer of regex glue. Each branch fails in its own way. The engineering cost is in maintaining that branching every time a new document type arrives.
One Pipeline for Every Branch: Parsing and Extracting With LlamaParse
LlamaParse replaces that branching with agentic OCR. Before anything is read, a layout-aware vision pass marks up the page: this block is body text, that region is a ruled table, this corner is a scanned signature, and that box is a bar chart. Agentic orchestration then picks a reader per region (a conventional OCR engine for clean print, a vision-language model for the table or the chart) and stitches the results back together in reading order.
You don't have to run a scan check, add Tesseract as a fallback, or write stitching code for a table that crosses a page break. The four-branch way to extract data from PDFs programmatically becomes one call:
from llama_cloud import LlamaCloud
client = LlamaCloud() # reads LLAMA_CLOUD_API_KEY from the environment
file = client.files.create(file="statement.pdf", purpose="parse")
result = client.parsing.parse(
file_id=file.id,
tier="agentic",
version="latest",
output_options={"markdown": {"tables": {"output_tables_as_markdown": True}}},
expand=["markdown"],
)
for page in result.markdown.pages:
print(page.markdown) # tables come back as Markdown tables, scans includedWhen you need fields instead of Markdown, LlamaParse takes a schema and returns JSON that conforms to it, so the regex layer disappears too. The schema below is that same statement record, transaction list included:
import time
from pydantic import BaseModel, Field
from llama_cloud import LlamaCloud
class Transaction(BaseModel):
date: str = Field(description="Posting date as printed")
description: str
amount: float = Field(description="Negative for debits")
class Statement(BaseModel):
account_number: str
opening_balance: float
closing_balance: float
transactions: list[Transaction]
client = LlamaCloud()
uploaded = client.files.create(file="statement.pdf", purpose="extract")
job = client.extract.create(
file_input=uploaded.id,
configuration={
"data_schema": Statement.model_json_schema(),
"extraction_target": "per_doc",
"tier": "agentic",
},
)
while job.status not in ("COMPLETED", "FAILED", "CANCELLED"):
time.sleep(2)
job = client.extract.get(job.id)
statement = job.extract_result # a dict shaped like StatementRun against a one-page statement that prints debits and credits in separate columns, it returns this (trimmed to the first three of ten transactions):
{
"account_number": "4417-0092-3381",
"opening_balance": 2104.87,
"closing_balance": 7166.42,
"transactions": [
{"date": "09/02/2026", "description": "Payroll Deposit ACME CORP", "amount": 3250.0},
{"date": "09/03/2026", "description": "Rent Payment - Oakview Apts", "amount": -1450.0},
{"date": "09/05/2026", "description": "Whole Foods Market #1021", "amount": -87.42}
]
}Nothing in the PDF has a minus sign. The "Negative for debits" description on the amount field is what folded two columns into one signed number, and that's why the balance check we wrote for the DIY code runs unchanged against statement and passes. For documents with hundreds of repeating rows, our post on extracting repeating entities covers row-level extraction targets, and parse vs. extract explains when you want each. For page targeting and chart parsing options, see our LlamaParse PDF parsing how-to.
Cost is concrete enough to weigh against engineering time. Parsing is priced per page by tier: Fast at 1 credit, Cost Effective at 3, Agentic at 10, and Agentic Plus at 25, with credits at $1.25 per thousand on the Pro plan and $1.50 pay-as-you-go.

For accuracy, we built ParseBench from insurance filings, financial reports, and government documents, about 2,000 pages in all, each checked by a person. LlamaParse Agentic tops it at 84.9% overall and 90.7% on tables, at about 1.2¢ to 1.5¢ per page. We wrote the test and we sell the product, so read our first-place finish with that in mind.
The leaderboard lives at parsebench.ai, the dataset and evaluation code are public, and the table-scoring method is worth using for your own evaluation. If you want an outside view, we keep running comparisons of AI PDF parsers, AI data extraction tools, document parsing APIs and automated document extraction software.
One caveat: for clean born-digital files where you only need text, pypdf is free, local, and fine. If you want something local that also handles the occasional scan, our open-source LiteParse runs on your machine with OCR built in. When documents have to stay in your own environment, LlamaParse can also be deployed in your own cloud account or data center on an Enterprise plan, and parsing on the Cost Effective and Agentic tiers can run on models you host. LlamaParse earns its cost once scans, tables, or vendor variety show up.
The Hardest Tenth of Your PDFs Decides the Stack
Any library looks good on a demo PDF. What you're actually choosing is who maintains the branching for the scanned, mixed, and table-heavy files that the classifier from the first section sorts out. In the pipelines we see, those files are usually the minority by count and the majority of the engineering time, because each new vendor or statement format adds another crop box, another regex, and another page-stitching rule.
So start with the measurement. Run classify() across a folder of the documents you actually receive and look at the size of the "scanned" and "mixed" buckets. If they're empty, keep your pypdf script. If they aren't, send that bin through LlamaParse and compare its tables against what your DIY branch produced, row for row, using the balance check as the referee. That's a fair test of how to extract data from PDFs programmatically on your own files, and you can run it on the 10,000 free credits you get at signup.
来源:LlamaIndex:产品、工程与评测 · llamaindex.ai