Skip to content

Repository files navigation

Automated Purchase Order / Invoice Data Extraction (OCR + Claude)

Author: Damilola Oguntoyinbo · Python + Tesseract OCR + Claude API Data: sample_invoices/ contains 3 fully synthetic scanned invoice images generated for this demo. No real vendor or financial data is used.

Business problem

Vendor invoices arrive as scanned images or photographed paper documents, not clean structured data. Manually re-keying vendor name, invoice number, PO reference, line items, and totals into a tracking sheet is slow and error-prone — this pipeline automates that step.

How it works — and why both stages matter

  1. Tesseract OCR (real, runs on every image) extracts raw text from each scanned invoice. This step is genuinely imperfect by design — the sample invoices are rendered with a slight scan-like rotation, and OCR introduces real mistakes: "AquaTech" gets misread as "AquatTech", table columns merge ("Unit Price (NGNje Total (NGN)"), and a "7.5%" VAT label gets read as "(7%)". You can see this for yourself in sample_output/raw_ocr_text/.
  2. Claude structures the noisy OCR text into a clean, validated JSON schema (vendor, invoice number, date, PO reference, line items, subtotal, VAT, total), using arithmetic consistency (qty × unit price = line total; subtotal + VAT = total) to catch and correct OCR errors rather than passing them through. This is the actual value-add over a plain OCR-to-CSV script — it recovers usable structured data from imperfect scans.

Files

File Purpose
extract_invoices.py Main pipeline — OCR, Claude structuring, CSV/JSON export
make_sample_invoices.py Generates the 3 synthetic sample invoice images
sample_invoices/ The 3 synthetic input images
sample_output/extracted_invoices.csv Final structured dataset (one row per invoice)
sample_output/extracted_invoices.json Same data, full detail incl. line items and OCR-correction notes
sample_output/raw_ocr_text/ Raw Tesseract output per invoice, for transparency
requirements.txt anthropic, pytesseract, Pillow (+ the tesseract-ocr system package)

Running it

pip install -r requirements.txt
# tesseract-ocr must also be installed at the OS level, e.g.:
# sudo apt-get install tesseract-ocr

# Live run (needs an Anthropic API key):
export ANTHROPIC_API_KEY=sk-ant-...
python extract_invoices.py --images-dir sample_invoices --out sample_output/extracted_invoices.csv

# No API key yet? Demo mode still runs real OCR on the images — only the
# Claude structuring step uses representative mocked output:
python extract_invoices.py --images-dir sample_invoices --out sample_output/extracted_invoices.csv --demo

Sample result

All 3 invoices extracted correctly despite OCR noise, totalling ₦44,410,400 across AquaTech Engineering Nigeria (₦3,323,900), Prime Solar Pumps Nigeria Ltd (₦9,288,000), and Delta Water & Power Ltd (₦31,798,500) — each tagged with its PO reference (PO-ACR-2025-014, PO-RAA-2025-022, PO-TRI-2025-005) so it could be reconciled directly against the Purchase Order tracker in the Excel/SQL projects in this portfolio.

About

Vendor invoice extraction pipeline: Tesseract OCR on scanned images, then the Claude API structures the noisy text into validated JSON and CSV.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages