Disclosure: I make BankPDF2Excel at Murabyte, which is mentioned at the end. Written with AI assistance and reviewed by Murabyte.
Most bank statement PDFs a client sends you are locked. The password is usually something the client already knows (part of an ID number, a date of birth, sometimes a phone number), and the bank tells them on the download page or in the email that delivered the file. That part is not a technical problem. The technical problem starts after you open it: a locked PDF that opens fine can still hand you half a statement.
Open it without pretending it is unprotected
from pypdf import PdfReader
reader = PdfReader("statement.pdf")
if reader.is_encrypted:
reader.decrypt("PASSWORD") # user password, not owner password
print(len(reader.pages))
text = reader.pages[0].extract_text()
Two things trip people up here. First, decrypt needs the user password, the one the bank gave the customer. The owner password is a different string used to set permissions, and it is not always the same. Second, pypdf decrypts into memory only; nothing is written back to disk, which is what you want.
If extract_text() returns an empty string on a page that clearly has text, the file is a scan and you need OCR, not a password. Check one page before you loop over two hundred.
Prove you got every page
A truncated statement is the expensive failure: the totals look plausible, the last two weeks are missing, and nobody notices until the reconciliation is off. The statement tells you how many pages it should have, on every page:
import re
first = reader.pages[0].extract_text()
m = re.search(r"Page\s+(\d+)\s+of\s+(\d+)", first)
expected = int(m.group(2)) if m else None
print("pages in file:", len(reader.pages), "expected:", expected)
If expected is larger than the page count, the download was cut short and you should ask for the file again rather than guess. If it is smaller, someone combined two statements and you now have a document whose header no longer matches its content.
Check the period, not just the page count
Page count catches truncation. It does not catch the wrong statement. Print the first and last transaction date from the extracted text and compare them with the period printed on page one. A statement for 1-30 November that ends on the 17th is not a short month; it is the wrong file, or a partial one from a download that stopped early.
dates = re.findall(r"\b\d{2}[/-]\d{2}[/-]\d{4}\b", text)
print(min(dates), max(dates))
That is crude and it is enough. Two dates and a page number catch most of the bad files before any conversion happens.
What not to do
Do not remove the protection and pass the file around. The password exists because the file is a bank record; open it, extract what you need, and keep the unlocked copy off shared drives. If you must keep one, keep it in the same place you keep the client's other financial documents, not in a downloads folder.
When it is worth automating
Doing this by hand for one statement is fine. Doing it for a folder of statements, each with its own password, from several banks, with scanned pages mixed in, is where the time goes. BankPDF2Excel opens the locked file with the password you type, converts each page to rows, flags rows it is unsure about with a confidence score, and exports XLSX, CSV, QBO or Tally XML. It runs on your machine, so the statement and the password never leave it. The trial exports the first 10 transactions of each statement so you can check a real file before paying anything.
Free trial: https://murabyte.gumroad.com/l/bankpdf2excel-trial
Gumroad: https://murabyte.gumroad.com/l/bankpdf2excel
Microsoft Store: https://apps.microsoft.com/detail/9N6R286B2LKP
python #pdf #banking #automation #murabyte
Written with AI assistance and reviewed by Murabyte.
Top comments (0)