Add WIP monthly debit and credit plot

This commit is contained in:
Benjamin Loison 2024-10-01 20:35:43 +02:00
parent 480072779c
commit b75d5fc86f
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8

View File

@ -1,10 +1,12 @@
#!/usr/bin/python3 #!/usr/bin/env python
# Depends on `pdftotext`. # Depends on `pdftotext`.
import os import os
import subprocess import subprocess
import re import re
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
path = f'/home/benjamin/Desktop/bens_folder/bazaar/documents/bnp/bank_statements/' path = f'/home/benjamin/Desktop/bens_folder/bazaar/documents/bnp/bank_statements/'
@ -27,12 +29,17 @@ def execute(command):
def getTextFromPdf(pdfPath): def getTextFromPdf(pdfPath):
return execute(['pdftotext', '-raw', pdfPath, '-']) return execute(['pdftotext', '-raw', pdfPath, '-'])
firstLineOfPaymentRegex = re.compile('\d{2}\.\d{2} \d{2}\.\d{2} \d+,\d{2}') firstLineOfPaymentRegex = re.compile('\\d{2}\\.\\d{2} \\d{2}\\.\\d{2} \\d+,\\d{2}')
endPageAfterTheFirstOneRegex = re.compile('P\. \d+/\d+') endPageAfterTheFirstOneRegex = re.compile('P\\. \\d+/\\d+')
soldeCrediteurAuRegex = re.compile('SOLDE CREDITEUR AU \d{2}\.\d{2}\.\d{4}') 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})')
for folder in os.listdir(): PRINT_TRANSACTIONS = False
for file in os.listdir(folder):
totalMonthlyDebits = []
totalMonthlyCredits = []
for folder in sorted(os.listdir()):
for file in sorted(os.listdir(folder)):
#folder = '2022' #folder = '2022'
#file = '20220321.pdf' #file = '20220321.pdf'
filePath = f'{folder}/{file}' filePath = f'{folder}/{file}'
@ -64,9 +71,15 @@ for folder in os.listdir():
continue continue
# We aren't interested in the content after this line # We aren't interested in the content after this line
elif line.startswith('TOTAL DES OPERATIONS'): 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 break
if firstLineOfPaymentRegex.match(line) is not None: if firstLineOfPaymentRegex.match(line) is not None:
if date is not None: if date is not None and PRINT_TRANSACTIONS:
print(date, valeur, amount, currentAmount) print(date, valeur, amount, currentAmount)
print('\n'.join(comment)) print('\n'.join(comment))
print() print()
@ -76,6 +89,19 @@ for folder in os.listdir():
comment = [] comment = []
else: else:
comment += [line] comment += [line]
break #break
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()