#!/usr/bin/env python import os import matplotlib.pyplot as plt import matplotlib.ticker as ticker from datetime import datetime from utils import getDatetimeFromFileName, getMonthIndexSinceEpoch, getMonthNameFromMonthIndex, readPdfBankStatement import operator from pprint import pprint PATH = f'/home/benjamin/Desktop/bens_folder/bazaar/documents/bnp/bank_statements/' os.chdir(PATH) PRINT_TRANSACTIONS = False MAIN_BANK_ACCOUNT = 'compte_de_cheques' allTransactions = [] monthlyTransactions = {} def appendTransactions(transactions, bankAccount): global allTransactions for transaction in transactions: transaction['bank account'] = bankAccount allTransactions += [transaction] date = transaction['date'].replace(day = 1) monthlyTransactions[date] = monthlyTransactions.get(date, []) + [transaction] for folder in os.listdir(): if folder != MAIN_BANK_ACCOUNT: for file in os.listdir(folder): filePath = f'{folder}/{file}' print(filePath) transactions = readPdfBankStatement(filePath)[3] pprint(transactions) appendTransactions(transactions, folder) os.chdir(f'{MAIN_BANK_ACCOUNT}/') for folder in sorted(os.listdir()): for file in sorted(os.listdir(folder)): filePath = f'{folder}/{file}' print(filePath) transactions = readPdfBankStatement(filePath)[3] appendTransactions(transactions, MAIN_BANK_ACCOUNT) if PRINT_TRANSACTIONS: pprint(transactions) #break #break allTransactions.sort(key = operator.itemgetter('date')) print(len(allTransactions)) #pprint(allTransactions) import re VIRT_A_CPTE_EMIS_SUR_LE_REGEX = re.compile('VIRT CPTE A CPTE EMIS SUR LE\n(CEL|LEP|LVJ|L\.A|LDD)\\d{23}') # Could precise bank account to restrict own account comments. def isTransactionFromOwnAccounts(comment): #if comment.startswith('DEPOT INITIAL DU COMPTE\n'): # print(comment) #if comment.startswith('VIR CPTE A CPTE EMIS /MOTIF '): # print(comment) return comment.startswith('DEPOT INITIAL DU COMPTE\n') or \ comment.startswith('VIR CPTE A CPTE EMIS /MOTIF ') or \ VIRT_A_CPTE_EMIS_SUR_LE_REGEX.fullmatch(comment)#comment.startswith('VIRT CPTE A CPTE EMIS SUR LE') #comment.startswith('VIR CPTE A CPTE RECU /DE ') or \ # and comment.endswith('/REFDO /REFBEN') #allTransactions = [transaction for transaction in allTransactions if not isTransactionFromOwnAccounts(transaction['comment'])] sortedMonths = sorted(monthlyTransactions.keys()) for month in sortedMonths: monthlyTransactions[month] = [transaction for transaction in monthlyTransactions[month] if not isTransactionFromOwnAccounts(transaction['comment'])] totalMonthlyDebits = [sum([min(transaction['amount'], 0) for transaction in monthlyTransactions[month]]) for month in sortedMonths] totalMonthlyCredits = [sum([max(transaction['amount'], 0) for transaction in monthlyTransactions[month]]) for month in sortedMonths] totalMonthlyDifferences = [sum([transaction['amount'] for transaction in monthlyTransactions[month]]) for month in sortedMonths] totals = [monthlyTransactions[sortedMonths[0]][0]['current amount'] + sum(totalMonthlyDifferences[:monthIndex + 1]) for monthIndex in range(len(sortedMonths))] fig, ax = plt.subplots() plt.title('BNP accounts monthly debits and credits') plt.xlabel('Date') plt.ylabel('€') ALPHA = 0.5 xTicks = range(getMonthIndexSinceEpoch(sortedMonths[0]), getMonthIndexSinceEpoch(sortedMonths[-1]) + 1) totalMonthlyAmountAndLabel = ( (totalMonthlyDebits, 'Debit'), (totalMonthlyCredits, 'Credit'), (totalMonthlyDifferences, 'Difference'), (totals, 'Total'), ) for totalMonthlyAmount, totalMonthlyLabel in totalMonthlyAmountAndLabel: plt.bar(xTicks, totalMonthlyAmount, alpha = ALPHA, label = totalMonthlyLabel) plt.legend() #plt.yscale('symlog') ax.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,}')) ticksLabels = [getMonthNameFromMonthIndex(monthIndex) for monthIndex in xTicks] plt.xticks(xTicks, ticksLabels, rotation = 90) #plt.tight_layout() # How to show the horizontal lines for subticks? plt.grid(axis = 'y') plt.show()