At the start of every month, the accounting colleague does the same thing: open dozens of PDF invoices that arrived by email, read the amount, the date and the invoice number on each one, then type them into Excel. Thirty invoices take about ninety minutes. A hundred takes half a day. One misplaced decimal point, and it takes a long time to work out which invoice went wrong.
That job can be handed to Python. Here is how it actually works: how to tell whether a PDF can be read at all, what to do when it cannot, and how to verify the numbers afterwards.
First, understand that PDFs come in three kinds
- With a text layer: exported from Word or a system, for example an accounting package or a supplier portal. The text can be selected and copied, and a program can read it. This is the ideal case.
- With table rules: text exists, but position is defined by ruled lines. Extracting these needs pdfplumber's extract_tables rather than plain text extraction.
- Scanned images: a photocopy or a photo saved as PDF. Inside is just a picture with no text at all. This needs OCR, and accuracy is always lower.
The first step is always to try copying. Open the PDF and drag-select a line of text. If it selects, there is a text layer. Test three files; if all three select, you can start writing code.
Step 1: install two packages
pdfplumber reads the PDF, openpyxl writes the xlsx. Both are pure Python, with nothing else to install.
Step 2: extract one file and look at the raw output
Do not write the batch job first. Open one typical invoice and see what extract_tables returns:
The output is usually a list of rows, each row a list of cells. If you see neat rows and columns, the table is readable. If every result is an empty list, the file is either a scan, or its "table" is drawn with background colour rather than real lines, and you need the text route instead.
Step 3: batch it and write to Excel
Assume the amount sits after the word Total, and the date uses a standard format. This script reads every PDF in a folder and assembles one spreadsheet:
Two details matter here. The amount is not the first number found but the largest, because an invoice also carries unit prices, subtotals and tax, and the grand total is usually the biggest. The row count comes from the file list, not from text inside the PDF, so a file that failed to parse still shows up as an empty row rather than silently disappearing.
When the output is messy: three fallback levels
Messy output is normal. No two suppliers lay out an invoice the same way. From most reliable to most flexible:
- Tables first: if extract_tables returns clean rows and columns, use it. It is the fastest and involves the least guessing.
- Keywords plus regex: if tables fail, take the full text and anchor on fixed words, for example whatever follows "Total", "Due date" or "Invoice No.".
- Coordinate cropping: if even the wording varies, use page.crop() to read one fixed region only.
If one supplier is especially awkward, do not complicate the whole script for them. Keep a small json of layout quirks, apply the special rule when that supplier appears, and let everything else run normally.
Verification: three rules, every single run
A wrong number is more dangerous than a missing one, because the error is invisible. After every run, do three things:
- Count matches: as many rows in Excel as PDFs in the folder. One short means one file did not parse.
- Total matches: the Excel sum must equal the manual sum, or the accounting system's total for the same period. This is the most effective single check, and errors usually surface immediately.
- Spot check: pick five at random and compare every field against the original PDF, decimals and date format in particular.
Only hand the file on once all three pass. If one fails, the cause is usually a handful of oddly laid out PDFs rather than faulty logic.
Scanned PDFs: OCR exists, but do not start there
Scans need OCR, such as PaddleOCR or Tesseract. In practice accuracy lands around ninety percent, with Chinese characters and decimal points the most common failures, so human review is mandatory. If only a few files a month are scans, using a PDF reader's text selection plus manual typing is usually faster than building an OCR pipeline, and it adds one less point of failure. Wait until scans pass thirty percent of the pile before investing the time.
Real numbers: a 12-person trading firm
About ninety supplier PDF invoices a month, roughly sixty percent with a text layer. Previously the accounting colleague spent fifty seconds on each, about seventy-five minutes a month. With the script above, a run takes forty seconds and review about fifteen minutes, finishing inside twenty. The time saved is not really typing speed. It is not opening and closing ninety files, and not hunting for the same field over and over.
Summary
Python Tutorial: extracting PDF data does not need complicated AI. Three steps carry it. Use text selection to tell which kind of PDF you have, use pdfplumber to pull either tables or full text, then verify with the count, the total and a spot check. Done properly, an hour of monthly retyping drops to twenty minutes. Skip the verification, and you will spend far longer chasing wrong numbers. Keep the code simple, but never skip the checks.