Python & AI Automation

Automating Invoice, PDF and Document Processing with Python and LLMs

Home Blog Python & AI Automation

Python & AI Automation

Finance and operations teams still spend hours every week copying numbers from PDFs into spreadsheets and ERPs. Large language models (LLMs) have made automating this far more practical, as long as they're combined with the right checks. Here's the pipeline we build.

Step 1: Get the text out

Documents come in two flavours:

  • Digital PDFs (generated by software) already contain text. Libraries like pdfplumber or PyMuPDF extract it, including table positions, quickly and for free.
  • Scanned documents and photos need OCR. Tesseract works for clean scans. Cloud OCR services or vision-capable LLMs handle messy, skewed or handwritten documents much better.

Always try the cheap, deterministic route first and fall back to OCR only when a page has no text layer.

Step 2: Extract structured fields with an LLM

Traditional template-based extraction breaks whenever a supplier changes its invoice layout. LLMs read invoices the way a person does. The key is to ask for strict, structured output:

from pydantic import BaseModel

class LineItem(BaseModel):
    description: str
    quantity: float
    unit_price: float
    amount: float

class Invoice(BaseModel):
    supplier_name: str
    invoice_number: str
    invoice_date: str        # ISO 8601
    currency: str
    subtotal: float
    tax: float
    total: float
    line_items: list[LineItem]

Most major LLM APIs now support structured or JSON-schema output, so the model returns data that fits this schema directly. That's much more reliable than parsing free text.

Step 3: Validate everything

LLMs are very good, but they're not infallible. Never write extracted data straight into your accounting system. Add rule-based checks:

  • Do the line items add up to the subtotal? Does subtotal + tax equal the total?
  • Is the invoice number already in the system (a duplicate)?
  • Does the supplier exist in your vendor master? Does the tax ID match?
  • Is the date plausible, and is the currency expected for this supplier?

These checks catch most extraction errors and real-world problems such as duplicate or fraudulent invoices.

Step 4: Human-in-the-loop review

Documents that pass every check can flow straight through. Anything that fails goes to a simple review screen that shows the original PDF next to the extracted fields, so a person can fix it in seconds. Over time you can track which suppliers or fields cause problems and improve prompts or rules.

Step 5: Push to your systems

Approved data is posted to your ERP or accounting software (Tally, Zoho Books, QuickBooks, Xero, SAP and others) through their APIs, or exported as CSV or Excel where no API exists.

What results look like

With validation and review in place, the typical outcome is that most documents need no human touch, and the rest take seconds instead of minutes. Just as important, every document has an audit trail: original file, extracted data, validation results and who approved it.

Beyond invoices

The same pipeline works for purchase orders, delivery notes, bank statements, insurance claims, KYC documents, contracts and application forms, anywhere people currently re-type data from documents.

Drowning in paperwork? Send us a few sample documents and we'll show you what can be automated.