The AI reads. The code decides.
One command processes a whole folder. Here is what happens to every PDF.
Read the PDF
The text layer is pulled out of each PDF. No text (a scan)? It goes in the log and the script moves on.
Ask the AI
Gemini returns 8 fields as structured data. The prompt forbids guessing: unknown means empty.
Check in code
Five checks run on the answer. Nothing the AI says is trusted until it passes them.
Write the CSV
One row per invoice, status "ok" or "needs review" with the reason, plus a log of every file.
What "needs review" actually means
Each example below is a real message from the sample run or the demo.
- 1. Subtotal + tax must equal totalDone by the script, tolerance one cent.
total mismatch: 3120.00 + 249.60 = 3369.60, invoice says 3396.60 - 2. Every amount must be written in the PDFStops the AI from "helpfully" correcting or inventing a figure. Tax may be the sum of up to three printed tax lines, such as GST + PST.
total 3369.60 not found in the PDF text - 3. Every date must be written in the PDF"Due on receipt" is not a due date, so it is not accepted as one.
missing: due_date - 4. Nothing missing, nothing malformedAll 8 fields are required. Dates must be real calendar dates (YYYY-MM-DD) and currency a 3-letter code. A due date before the invoice date, or identical to it, is flagged.
- 5. Unreadable files are listed, never guessedScans and corrupt PDFs are written to the log as SKIPPED. An AI outage is logged as FAILED, and running the script again retries it.
Three commands
pip install -r requirements.txtcopy .env.example .env # paste your keypython process_invoices.py "C:\Invoices\May"
Output: invoices.csv and processing_log.txt, next to the script.
No surprise bill
- You use your own Google AI Studio key and are billed directly. Set a monthly budget alert in Google Cloud first.
- Only the invoice text is sent, one PDF at a time. Check this suits your clients' confidentiality terms.
- The key lives in a local
.envfile and is never in the code.
What it will not do
It is not 100% accurate
The checks catch arithmetic, missing and invented numbers. A wrong vendor name or invoice number can still slip through, so skim the CSV before importing.
Text PDFs only
Some vendors still send scans. Those are logged for manual entry. OCR could be a small follow-up project.
Folder in, CSV out
No Gmail, Google Sheets or accounting software connection. Kept small on purpose so it works.