Prompt
How do I get key-value pairs out of invoices with OCR?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To extract key-value pairs from invoices with OCR, you usually need two steps:
- OCR text extraction
Convert the invoice image/PDF into machine-readable text. - Field extraction / key-value parsing
Map the OCR text into structured fields likeinvoice_number,date,vendor_name,total, etc.
Common approaches
1) Rule-based parsing
Best if your invoices are from a few known vendors/templates.
- Use OCR to get text
- Search for labels like:
Invoice No:Date:Total:
- Extract values using regex or position-based rules
Pros: simple, cheap, fast
Cons: brittle across varying invoice layouts
2) Template-based extraction
Best when invoice formats are consistent.
- Detect layout regions
- OCR each region
- Associate labels with nearby values based on coordinates
This works well if you know the invoice structure in advance.
3) ML / document AI extraction
Best for many vendors and varying layouts.
Use specialized document understanding tools/models that do:
- OCR
- layout analysis
- key-value extraction
Examples:
- Amazon Textract
- Google Document AI
- Azure Form Recognizer / Document Intelligence
- Open-source models like LayoutLM, Donut, or custom pipelines
Pros: handles many formats better
Cons: more setup, training, or cost
Typical pipeline
-
Preprocess image
- deskew
- denoise
- increase contrast
-
Run OCR
- Tesseract, PaddleOCR, EasyOCR, cloud OCR, etc.
-
Extract text + coordinates
- You want both the words and their bounding boxes
-
Find labels and values
- Example: if
Invoice Number:appears, capture text to the right of it - If
Totalappears near a currency amount, pair them
- Example: if
-
Normalize output
- Convert to JSON:
{ "invoice_number": "INV-10293", "invoice_date": "2026-10-01", "vendor": "Acme Supplies", "total": "$1,245.50" }
If you’re using OCR text only
You can extract key-value pairs with regex patterns such as:
import re
text = """
Invoice No: INV-10293
Date: 2026-10-01
Total: $1,245.50
"""
invoice_no = re.search(r'Invoice\s*No[:\s]+(\S+)', text)
date = re.search(r'Date[:\s]+([0-9\-\/]+)', text)
total = re.search(r'Total[:\s]+\$?([0-9,]+\.\d{2})', text)
print({
"invoice_number": invoice_no.group(1) if invoice_no else None,
"date": date.group(1) if date else None,
"total": total.group(1) if total else None
})
If you want better accuracy
Use OCR with bounding boxes. Then apply heuristics like:
- value is to the right of the label
- value is on the next line
- use the nearest numeric text for totals
- use layout-aware models for complex invoices
Recommended tools
- Simple OCR: Tesseract, EasyOCR, PaddleOCR
- Best out-of-the-box invoice extraction: AWS Textract, Google Document AI, Azure Document Intelligence
- Open-source advanced: LayoutLM, Donut
If you want, I can show you:
- a Python example using Tesseract,
- a cloud API approach, or
- a layout-aware ML approach for invoices.