#!/usr/bin/env python # Depends on `pdftotext`. import os import subprocess import re import matplotlib.pyplot as plt import matplotlib.ticker as ticker path = f'/home/benjamin/Desktop/bens_folder/bazaar/documents/bnp/bank_statements/' os.chdir(path) ''' Assuming file hierarchy like: 2022 ├── 20221121.pdf └── 20221221.pdf 2023 ├── 20230123.pdf └── 20230221.pdf ''' def execute(command): return subprocess.check_output(command).decode('utf-8') def getTextFromPdf(pdfPath): return execute(['pdftotext', '-raw', pdfPath, '-']) firstLineOfPaymentRegex = re.compile('\\d{2}\\.\\d{2} \\d{2}\\.\\d{2} \\d+,\\d{2}') endPageAfterTheFirstOneRegex = re.compile('P\\. \\d+/\\d+') soldeCrediteurAuRegex = re.compile('SOLDE CREDITEUR AU \\d{2}\\.\\d{2}\\.\\d{4}') totalDesOperationsRegex = re.compile('TOTAL\\ DES\\ OPERATIONS\\ ([0-9 ]+,\\d{2})\\ ([0-9 ]+,\\d{2})') PRINT_TRANSACTIONS = False totalMonthlyDebits = [] totalMonthlyCredits = [] for folder in sorted(os.listdir()): for file in sorted(os.listdir(folder)): #folder = '2022' #file = '20220321.pdf' filePath = f'{folder}/{file}' print(filePath) content = getTextFromPdf(filePath) lines = content.splitlines() started = False firstPage = True initialAmount = None currentAmount = None date = None comment = [] for line in lines: if not started: # We are interested in the content after this line: if soldeCrediteurAuRegex.match(line) is not None or (line.startswith('Date Nature des opérations Valeur Débit Crédit') and not firstPage): if soldeCrediteurAuRegex.match(line): initialAmount = float(soldeCrediteurAuRegex.sub('', line).replace(',', '.').replace(' ', '')) currentAmount = initialAmount print('Initial amount', initialAmount) print() started = True continue else: # We aren't interested in the content after this line: if line.startswith('BNP PARIBAS SA au capital de') or endPageAfterTheFirstOneRegex.match(line) is not None: firstPage = False started = False continue # We aren't interested in the content after this line elif line.startswith('TOTAL DES OPERATIONS'): totalDesOperationsRegexMatch = totalDesOperationsRegex.match(line) totalMonthlyDebit, totalMonthlyCredit = [float(group.replace(',', '.').replace(' ', '')) for group in totalDesOperationsRegexMatch.groups()] print(f'Total monthly debit: {totalMonthlyDebit}') print(f'Total monthly credit: {totalMonthlyCredit}') totalMonthlyDebits += [totalMonthlyDebit] totalMonthlyCredits += [totalMonthlyCredit] break if firstLineOfPaymentRegex.match(line) is not None: if date is not None and PRINT_TRANSACTIONS: print(date, valeur, amount, currentAmount) print('\n'.join(comment)) print() date, valeur, amount = line.split() amount = float(amount.replace(',', '.')) currentAmount -= amount comment = [] else: comment += [line] #break #break fig, ax = plt.subplots() plt.title('Monthly debits and credits') plt.xlabel('Date') plt.ylabel('€') ALPHA = 0.5 xTicks = range(len(totalMonthlyDebits)) plt.bar(xTicks, totalMonthlyDebits, alpha = ALPHA, label = 'Debit') plt.bar(xTicks, totalMonthlyCredits, alpha = ALPHA, label = 'Credit') plt.legend() ax.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,}')) plt.show()