BNP_PDF_statement_parser/bnp_pdf_statement_parser.py

96 lines
3.4 KiB
Python
Executable File

#!/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
PATH = f'/home/benjamin/Desktop/bens_folder/bazaar/documents/bnp/bank_statements/'
os.chdir(PATH)
PRINT_TRANSACTIONS = False
MAIN_BANK_ACCOUNT = 'compte_de_cheques'
# As far as I know there was no debit yet.
otherBankAccountsCredits = {}
for folder in os.listdir():#[-2:]:
if folder != MAIN_BANK_ACCOUNT:
print(folder)
for file in os.listdir(folder):#[-1:]:
print(file)
filePath = f'{folder}/{file}'
initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime = readPdfBankStatement(filePath)
#print(totalMonthlyCredit)
#import json
#print(json.dumps(transactions, indent = 4))
otherBankAccountsCredits[fileDatetime] = otherBankAccountsCredits.get(fileDatetime, 0) + totalMonthlyCredit
#exit(1)
os.chdir(f'{MAIN_BANK_ACCOUNT}/')
totalMonthlyDebits = []
totalMonthlyCredits = []
totalMonthlyDifferences = []
totals = []
firstDatetime = None
lastDatetime = None
for folder in sorted(os.listdir()):
for file in sorted(os.listdir(folder)):
filePath = f'{folder}/{file}'
print(filePath)
initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime = readPdfBankStatement(filePath)
print('Initial amount', initialAmount)
print()
totals += [initialAmount]
print(f'Total monthly debit: {totalMonthlyDebit}')
print(f'Total monthly credit: {totalMonthlyCredit}')
totalMonthlyDebits += [totalMonthlyDebit]
otherBankAccountsCredit = otherBankAccountsCredits.get(fileDatetime, 0)
print(f'{otherBankAccountsCredit=}')
totalMonthlyCredit += otherBankAccountsCredit
totalMonthlyCredits += [totalMonthlyCredit]
totalMonthlyDifference = totalMonthlyCredit - totalMonthlyDebit
totalMonthlyDifferences += [totalMonthlyDifference]
if PRINT_TRANSACTIONS:
for transaction in transactions:
# , transaction['currentAmount']
print(transaction['date'], transaction['valeur'], transaction['amount'])
print(transaction['comment'])
print()
if firstDatetime is None:
firstDatetime = fileDatetime
#break
#break
lastDatetime = getDatetimeFromFileName(file)
fig, ax = plt.subplots()
plt.title('Monthly debits and credits')
plt.xlabel('Date')
plt.ylabel('')
ALPHA = 0.5
xTicks = range(getMonthIndexSinceEpoch(firstDatetime), getMonthIndexSinceEpoch(lastDatetime) + 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()