BNP_PDF_statement_parser/bnp_pdf_statement_parser.py

141 lines
5.2 KiB
Python
Executable File

#!/usr/bin/env python
# Depends on `pdftotext`.
import os
import subprocess
import re
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from datetime import datetime
PATH = f'/home/benjamin/Desktop/bens_folder/bazaar/documents/bnp/bank_statements/compte_de_cheques/'
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, '-'])
FIRST_LINE_OF_PAYMENT_REGEX = re.compile('\\d{2}\\.\\d{2} \\d{2}\\.\\d{2} \\d+,\\d{2}')
END_PAGE_AFTER_THE_FIRST_ONE_REGEX = re.compile('P\\. \\d+/\\d+')
SOLDE_CREDITEUR_AU_REGEX = re.compile('SOLDE CREDITEUR AU \\d{2}\\.\\d{2}\\.\\d{4}')
TOTAL_DES_OPERATIONS_REGEX = re.compile('TOTAL\\ DES\\ OPERATIONS\\ ([0-9 ]+,\\d{2})\\ ([0-9 ]+,\\d{2})')
PRINT_TRANSACTIONS = False
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)
currentDatetime = getDatetime(file)
if firstDatetime is None:
firstDatetime = currentDatetime
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 SOLDE_CREDITEUR_AU_REGEX.match(line) is not None or (line.startswith('Date Nature des opérations Valeur Débit Crédit') and not firstPage):
if SOLDE_CREDITEUR_AU_REGEX.match(line):
initialAmount = float(SOLDE_CREDITEUR_AU_REGEX.sub('', line).replace(',', '.').replace(' ', ''))
currentAmount = initialAmount
print('Initial amount', initialAmount)
print()
totals += [initialAmount]
started = True
continue
else:
# We aren't interested in the content after this line:
if line.startswith('BNP PARIBAS SA au capital de') or END_PAGE_AFTER_THE_FIRST_ONE_REGEX.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 = TOTAL_DES_OPERATIONS_REGEX.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]
totalMonthlyDifference = totalMonthlyCredit - totalMonthlyDebit
totalMonthlyDifferences += [totalMonthlyDifference]
break
if FIRST_LINE_OF_PAYMENT_REGEX.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
lastDatetime = getDatetime(file)
fig, ax = plt.subplots()
plt.title('Monthly debits and credits')
plt.xlabel('Date')
plt.ylabel('')
ALPHA = 0.5
def getDatetime(aDatetimeStr):
return datetime.strptime(aDatetimeStr, '%Y%m%d.pdf')
def getMonthIndex(aDatetime):
return aDatetime.year * 12 + aDatetime.month
xTicks = range(getMonthIndex(firstDatetime), getMonthIndex(lastDatetime) + 1)
# sign does not seem respected for `totalMonthlyDifferences`.
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:,}'))
def getMonthName(monthIndex):
return datetime((monthIndex - 1) // 12, 1 + (monthIndex - 1) % 12, 1).strftime('%b %Y')
ticksLabels = [getMonthName(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()