BNP_PDF_statement_parser/bnp_pdf_statement_parser.py

96 lines
3.4 KiB
Python
Raw Normal View History

2024-10-01 20:35:43 +02:00
#!/usr/bin/env python
2023-06-21 00:36:27 +02:00
2023-11-05 21:10:45 +01:00
import os
2024-10-01 20:35:43 +02:00
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
2024-10-01 20:57:36 +02:00
from datetime import datetime
2024-10-03 19:51:54 +02:00
from utils import getDatetimeFromFileName, getMonthIndexSinceEpoch, getMonthNameFromMonthIndex, readPdfBankStatement
2023-06-21 00:36:27 +02:00
2024-10-03 19:33:54 +02:00
PATH = f'/home/benjamin/Desktop/bens_folder/bazaar/documents/bnp/bank_statements/'
2023-06-21 00:36:27 +02:00
2024-10-03 17:43:44 +02:00
os.chdir(PATH)
2023-06-21 00:36:27 +02:00
PRINT_TRANSACTIONS = False
2024-10-01 20:35:43 +02:00
2024-10-03 19:33:54 +02:00
MAIN_BANK_ACCOUNT = 'compte_de_cheques'
# As far as I know there was no debit yet.
otherBankAccountsCredits = {}
for folder in os.listdir():#[-2:]:
2024-10-03 19:33:54 +02:00
if folder != MAIN_BANK_ACCOUNT:
print(folder)
for file in os.listdir(folder):#[-1:]:
2024-10-03 19:33:54 +02:00
print(file)
2024-10-03 20:10:06 +02:00
filePath = f'{folder}/{file}'
initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime = readPdfBankStatement(filePath)
#print(totalMonthlyCredit)
#import json
#print(json.dumps(transactions, indent = 4))
2024-10-03 20:10:06 +02:00
otherBankAccountsCredits[fileDatetime] = otherBankAccountsCredits.get(fileDatetime, 0) + totalMonthlyCredit
2024-10-03 19:33:54 +02:00
2024-10-03 20:10:06 +02:00
#exit(1)
2024-10-03 19:33:54 +02:00
os.chdir(f'{MAIN_BANK_ACCOUNT}/')
2024-10-01 20:35:43 +02:00
totalMonthlyDebits = []
totalMonthlyCredits = []
2024-10-02 00:33:11 +02:00
totalMonthlyDifferences = []
totals = []
2024-10-01 20:57:36 +02:00
firstDatetime = None
lastDatetime = None
2024-10-01 20:35:43 +02:00
for folder in sorted(os.listdir()):
for file in sorted(os.listdir(folder)):
2023-06-21 00:36:27 +02:00
filePath = f'{folder}/{file}'
print(filePath)
2024-10-03 19:51:54 +02:00
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
2024-10-03 19:51:54 +02:00
totalMonthlyCredits += [totalMonthlyCredit]
totalMonthlyDifference = totalMonthlyCredit - totalMonthlyDebit
totalMonthlyDifferences += [totalMonthlyDifference]
if PRINT_TRANSACTIONS:
for transaction in transactions:
# , transaction['currentAmount']
print(transaction['date'], transaction['valeur'], transaction['amount'])
2024-10-03 20:10:06 +02:00
print(transaction['comment'])
2024-10-03 19:51:54 +02:00
print()
2024-10-01 20:57:36 +02:00
if firstDatetime is None:
2024-10-03 19:33:54 +02:00
firstDatetime = fileDatetime
2024-10-01 20:35:43 +02:00
#break
#break
lastDatetime = getDatetimeFromFileName(file)
2024-10-01 20:35:43 +02:00
fig, ax = plt.subplots()
plt.title('Monthly debits and credits')
plt.xlabel('Date')
plt.ylabel('')
ALPHA = 0.5
2024-10-01 20:57:36 +02:00
xTicks = range(getMonthIndexSinceEpoch(firstDatetime), getMonthIndexSinceEpoch(lastDatetime) + 1)
2024-10-02 00:33:11 +02:00
totalMonthlyAmountAndLabel = (
#(totalMonthlyDebits, 'Debit'),
#(totalMonthlyCredits, 'Credit'),
(totalMonthlyDifferences, 'Difference'),
(totals, 'Total'),
)
for totalMonthlyAmount, totalMonthlyLabel in totalMonthlyAmountAndLabel:
plt.bar(xTicks, totalMonthlyAmount, alpha = ALPHA, label = totalMonthlyLabel)
2024-10-01 20:35:43 +02:00
plt.legend()
2024-10-02 00:33:11 +02:00
#plt.yscale('symlog')
2024-10-01 20:35:43 +02:00
ax.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,}'))
2023-06-21 00:36:27 +02:00
ticksLabels = [getMonthNameFromMonthIndex(monthIndex) for monthIndex in xTicks]
2024-10-01 20:57:36 +02:00
plt.xticks(xTicks, ticksLabels, rotation = 90)
#plt.tight_layout()
# How to show the horizontal lines for subticks?
plt.grid(axis = 'y')
2024-10-01 20:35:43 +02:00
plt.show()