DEV Community

Murabyte
Murabyte

Posted on

Check a converted bank statement with pandas: verify the running balance row by row

Disclosure: I make BankPDF2Excel at Murabyte, which is mentioned at the end. Written with AI assistance and reviewed by Murabyte.

After you convert a bank statement from PDF to a spreadsheet, how do you know the rows are right? One missed or misread line changes every balance below it. This pandas script checks the converted file against the running balance, which is the best test you have.

The idea

Each row's balance should equal the previous balance plus credits minus debits. If a row breaks that rule, a number or a sign was misread on that row or the one above it.

The data

Export the converted statement to CSV with columns date, description, debit, credit, balance.

import pandas as pd

df = pd.read_csv("statement.csv")
for c in ["debit", "credit", "balance"]:
    df[c] = pd.to_numeric(df[c], errors="coerce").fillna(0) if c != "balance" else pd.to_numeric(df[c], errors="coerce")
Enter fullscreen mode Exit fullscreen mode

Check every row

df["expected"] = df["balance"].shift(1) + df["credit"] - df["debit"]
df["diff"] = (df["balance"] - df["expected"]).round(2)
bad = df[(df["diff"].abs() > 0.01) & df["expected"].notna()]
print(bad[["date", "description", "debit", "credit", "balance", "diff"]])
Enter fullscreen mode Exit fullscreen mode

Read the result

  • No rows printed: every line is consistent with the balance column.
  • One row with a diff equal to its own amount: probably a debit read as a credit, or the reverse.
  • A run of rows with the same diff: one row above them is missing or wrong, and everything after it carries the error.

Check the totals too

Compare the opening balance plus all credits minus all debits with the closing balance printed on the statement.

opening = df["balance"].iloc[0] - df["credit"].iloc[0] + df["debit"].iloc[0]
closing = df["balance"].iloc[-1]
print(round(opening + df["credit"].sum() - df["debit"].sum() - closing, 2))
Enter fullscreen mode Exit fullscreen mode

A result of 0.0 means the whole statement adds up.

Decimal commas and Dr/Cr columns

European statements use 1.234,56. Convert before parsing: s.str.replace(".", "", regex=False).str.replace(",", ".", regex=False). Some Gulf statements use Dr and Cr markers instead of two columns, so split the sign out first.

Where a tool helps

If you do this for many statements, BankPDF2Excel is a Windows app that converts PDF bank statements to Excel, CSV, QuickBooks QBO and Tally files on your own PC, shows a confidence flag per row with a side-by-side view so you can verify each line, and handles US, UK, Gulf and EU layouts. It is $149 per year with a 15-day trial that exports the first 10 transactions per statement: https://murabyte.gumroad.com/l/bankpdf2excel

Top comments (0)