Prompt
How do I extract line items from invoices and receipts?
Latest observation
To extract line items from invoices and receipts, you typically use a mix of OCR, document structure detection, and table/line-item parsing.
Common approach
-
Input the document
- PDF, scanned image, photo, email attachment, etc.
-
Run OCR
- Convert the image/PDF text into machine-readable text.
- Good OCR tools also return bounding boxes for each word/line.
-
Detect layout
- Identify regions such as:
- header
- vendor info
- total/subtotal/tax
- line-item table
- This helps separate item rows from the rest of the document.
- Identify regions such as:
-
Extract line items
- For invoices, line items are often in tables with columns like:
- description
- quantity
- unit price
- amount
- For receipts, line items may be more free-form, so you may need heuristics or an ML model.
- For invoices, line items are often in tables with columns like:
-
Post-process
- Normalize currency, quantities, dates
- Merge broken lines
- Validate totals against subtotal/tax/total
Methods you can use
1. Rules-based parsing
Best when invoice formats are consistent.
- Use OCR text and regexes to find table-like sections
- Detect rows using spacing or alignment
- Extract columns based on positions
Pros: simple, fast
Cons: fragile across different layouts
2. Template-based extraction
Best when you receive documents from a limited set of vendors.
- Build a parser for each invoice template
- Use coordinates to extract known fields and line items
Pros: high accuracy for known formats
Cons: doesn’t scale well to many vendors
3. ML / Document AI models
Best for many different invoice/receipt formats.
- Use document understanding models that combine OCR + layout + NLP
- Examples:
- Google Document AI
- AWS Textract
- Azure Document Intelligence
- open-source models like LayoutLM-based pipelines
Pros: works across varied layouts
Cons: more setup, cost, training/tuning may be needed
4. LLM-assisted extraction
Useful when line items are messy or OCR output is imperfect.
- Feed OCR text plus layout info to an LLM
- Ask it to return structured JSON
- Use strict schema validation afterward
Pros: flexible
Cons: must guard against hallucinations; validate carefully
What a line item schema might look like
{
"description": "Widget A",
"quantity": 2,
"unit_price": 5.00,
"amount": 10.00
}
For receipts, you might only reliably get:
{
"description": "Coffee",
"amount": 4.75
}
Practical tips
- Use OCR with coordinates, not plain text alone, if possible
- Preserve line breaks and spacing
- Handle multi-line descriptions
- Watch for totals that appear in the line-item area
- Verify extracted amounts against subtotal/total
- Use confidence scores and fallback logic for uncertain rows
Example workflow
If you want something production-ready:
- Upload invoice/receipt image or PDF
- OCR it with layout output
- Detect the line-item table region
- Parse rows into structured fields
- Validate totals
- Return JSON
If you want to build it yourself
A basic Python pipeline might use:
pytesseractoreasyocrfor OCRpdfplumberfor text-based PDFsopencvfor image preprocessing- regex + heuristics for row extraction
- optional LLM for cleanup/structuring
If you want, I can show you:
- a Python example for extracting line items,
- a recommended API/tool stack, or
- how to do it for scanned PDFs specifically.