Skip to content

Instantly share code, notes, and snippets.

@arssher
Last active March 14, 2025 21:00
Show Gist options
  • Select an option

  • Save arssher/20d1fc492978da6cf53496992685f997 to your computer and use it in GitHub Desktop.

Select an option

Save arssher/20d1fc492978da6cf53496992685f997 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import camelot
import argparse
from decimal import *
if __name__ == "__main__":
description = """
Try to parse Forte bank statement file and calculate the total debit and credit.
Usage:
sudo apt-get install ghostscript
create and activate some venv
pip install ghostscript
pip install "camelot-py[base]"
python parse_forte_stmt.py -f <pdf file>
"""
parser = argparse.ArgumentParser(description=description, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-f", "--file", help="pdf file to parse")
args = parser.parse_args()
fname = args.file
tables = camelot.read_pdf(fname, pages='all')
print(tables)
debit = 0
debits = []
credit = 0
credits = []
for t in tables:
print(t.df)
print(t.df.columns.values)
sum_col = t.df.iloc[:, 1]
sum_col_list = list(sum_col)
if sum_col_list[0] != "Sum":
print(f"expected first row in the sum column to be 'Sum', but it is {sum_col_list[0]}")
sys.exit(1)
for val in sum_col_list[1:]:
# print(f"considering {val}")
val = val.split()[0] # remove currency etc
f = Decimal(val)
# print(f"adding {f}")
if f >= 0:
debit += f
debits.append(f)
else:
credit -= f
credits.append(-f)
debits_str = [str(d) for d in debits]
credits_str = [str(c) for c in credits]
print(f"debits: {debits_str}")
print(f"credits: {credits_str}")
diff = debit - credit
print(f"{fname} total debit: {debit}, total credit: {credit}, diff: {diff}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment