Bank statements are one of the most common PDFs people want to turn into data. Accountants want them in a spreadsheet, freelancers want to tag expenses, and developers get asked to "just pull the transactions out". It sounds like a ten-minute job. It rarely is.
This post walks through a practical way to extract transaction tables from text-based statement PDFs with Python and pdfplumber, and then covers the edge cases that break most first attempts.
Step 0: check what kind of PDF you have
Before writing any parsing code, find out whether the PDF has a text layer.
import pdfplumber with pdfplumber.open("statement.pdf") as pdf: first = pdf.pages[0] text = first.extract_text() or "" print(len(text), "characters on page 1") If you get a few thousand characters, you have a "born-digital" PDF exported by the bank, and you can parse it directly. If you get zero or a handful of characters, the statement is a scan or a photo, and you need OCR first (for example ocrmypdf input.pdf output.pdf) before any of the steps below will work.
Step 1: try the table extractor first
Many banks draw real table lines, and pdfplumber can use them:
rows = [] with pdfplumber.open("statement.pdf") as pdf: for page in pdf.pages: for table in page.extract_tables(): rows.extend(table) for r in rows[:10]: print(r) If the output looks like ['03/11', 'CARD PAYMENT TESCO', '', '12.40', '1,204.11'], you are in luck. Most statements, however, use whitespace instead of ruled lines, and extract_tables() returns nothing useful. In that case, switch to text-based settings:
settings = { "vertical_strategy": "text", "horizontal_strategy": "text", "snap_tolerance": 3, } table = page.extract_table(settings) The text strategy infers columns from how the words line up. It works surprisingly well on clean layouts and badly on dense ones, so always eyeball the result for one page before trusting it for fifty.
Step 2: when tables fail, use word positions
The most reliable approach for messy statements is to work with word coordinates yourself. extract_words() gives you every word with its x0, x1 and top position:
words = page.extract_words(keep_blank_chars=False, use_text_flow=False) Group words into lines by rounding top, then assign each word to a column based on its x position. You can find the column boundaries once by looking at the header row ("Date", "Description", "Paid out", "Paid in", "Balance"):
from collections import defaultdict def group_lines(words, tol=3): lines = defaultdict(list) for w in words: key = round(w["top"] / tol) lines[key].append(w) return [sorted(ws, key=lambda w: w["x0"]) for _, ws in sorted(lines.items())] def to_columns(line, bounds): cols = [""] * (len(bounds) + 1) for w in line: idx = sum(w["x0"] >= b for b in bounds) cols[idx] = (cols[idx] + " " + w["text"]).strip() return cols Here bounds is a list of x positions that separate the columns, for example [90, 330, 410, 490]. Hard-coding them per bank is fine; statement layouts change rarely.
Step 3: the edge cases that will bite you
This is where most of the time goes.
Multi-line descriptions. A single transaction often wraps onto two or three lines, with the date and amounts only on the first. Treat any line without a date and without amounts as a continuation and append it to the previous row's description.
Dates without a year. Many statements print 03 Nov and put the year only in the header. Read the statement period from page 1 and attach the year yourself. Watch the December to January boundary: a statement covering 15 Dec to 14 Jan contains two years.
Debit and credit in one column. Some banks use a single amount column with a minus sign, others use CR/DR suffixes, and others use separate "Paid out" and "Paid in" columns. Normalise everything to one signed number early:
import re from decimal import Decimal def parse_amount(s): s = s.replace(",", "").strip() if not s: return None sign = -1 if s.endswith("DR") or s.startswith("-") or s.startswith("(") else 1 num = re.sub(r"[^0-9.]", "", s) return sign * Decimal(num) Use Decimal, not float. Floating-point rounding errors are small, but a reconciliation that is off by 0.01 will cost you an afternoon.
Thousand separators and locale. A European statement may print 1.204,11. Detect the locale from the bank or currency and swap separators before parsing.
Repeated headers and footers. Every page repeats the column header, and often a "Balance brought forward" line. Filter them out by text match, but keep the brought-forward balance: you need it for validation.
Page totals. Some statements print subtotals at the bottom of each page. They look exactly like transactions to a naive parser.
Step 4: validate with the running balance
The single best check is the balance column. Starting from the opening balance, add each signed amount and compare with the printed balance on every row:
balance = opening_balance for row in rows: balance += row["amount"] if row["balance"] is not None and balance != row["balance"]: print("Mismatch at", row["date"], row["description"]) break If this passes for the whole statement, your extraction is almost certainly correct. If it fails, the first mismatch points straight at the line your parser misread, which is usually a wrapped description or a subtotal.
Step 5: export
Once the rows validate, writing CSV is trivial:
import csv with open("transactions.csv", "w", newline="") as f: w = csv.DictWriter(f, fieldnames=["date", "description", "amount", "balance"]) w.writeheader() w.writerows(rows) When not to write code
If you only need one or two statements in a spreadsheet, writing a parser is overkill. A converter that turns the PDF tables into an Excel file will usually get you 90% of the way, and you can tidy the rest by hand. For that, there is the free PDF to Excel converter on HuntPDF (disclosure: this DEV account is run by the HuntPDF team), which works in the browser without an account. It still benefits from the same running-balance check described above, so do not skip validation just because a tool did the extraction.
Privacy note
Bank statements contain account numbers, addresses and spending history. If you build a pipeline, process files locally where you can, avoid logging raw text, and delete intermediate files when you are done. If you use any online tool, read its retention policy first.
Summary
- Check for a text layer first; OCR scans before parsing.
- Try
extract_tables(), then thetextstrategy, then raw word coordinates. - Handle wrapped descriptions, missing years, signed amounts and locales explicitly.
- Validate every row against the printed running balance.
That last step is what turns "it looks right" into "it is right".
This article was written with AI tools for the HuntPDF team.

