diff --git a/bnp_pdf_statement_parser.py b/bnp_pdf_statement_parser.py index 58259b9..337b2b0 100755 --- a/bnp_pdf_statement_parser.py +++ b/bnp_pdf_statement_parser.py @@ -53,29 +53,42 @@ allTransactions.sort(key = operator.itemgetter('date')) print(len(allTransactions)) #pprint(allTransactions) -amount = X_XXX.XX -for transaction in allTransactions: - if transaction['bank account'] == MAIN_BANK_ACCOUNT: - amount += transaction['amount'] - print(transaction['date'], amount, transaction['amount']) -print(amount) +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.match(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()) -#pprint(sortedMonths) +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('Monthly debits and credits') +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'), + (totalMonthlyDebits, 'Debit'), + (totalMonthlyCredits, 'Credit'), (totalMonthlyDifferences, 'Difference'), - #(totals, 'Total'), + (totals, 'Total'), ) for totalMonthlyAmount, totalMonthlyLabel in totalMonthlyAmountAndLabel: plt.bar(xTicks, totalMonthlyAmount, alpha = ALPHA, label = totalMonthlyLabel) diff --git a/utils.py b/utils.py index 99b0556..0e317bc 100644 --- a/utils.py +++ b/utils.py @@ -45,7 +45,7 @@ def readPdfBankStatement(filePath): firstPage = True initialAmount = None initialDate = None - #currentAmount = None + currentAmount = None date = None comment = [] transactions = [] @@ -64,7 +64,7 @@ def readPdfBankStatement(filePath): initialDate = datetime.strptime(soldeCrediteurAuRegexMatch.group(1), '%d.%m.%Y') initialAmount = toFloat(soldeCrediteurAuRegexMatch.group(2)) print(f'{initialAmount=}') - #currentAmount = initialAmount + currentAmount = initialAmount started = True continue else: @@ -95,7 +95,7 @@ def readPdfBankStatement(filePath): 'date': getDateFollowing(date, initialDate), 'valeur': getDateFollowing(valeur, initialDate), 'amount': amount, - #'currentAmount': currentAmount, + 'current amount': currentAmount, 'comment': '\n'.join(comment) }] date = None @@ -104,7 +104,7 @@ def readPdfBankStatement(filePath): amount = toFloat(amount) if abs(debitIndex - lineLen) < abs(creditIndex - lineLen): amount *= -1 - #currentAmount += amount + currentAmount += amount comment = [firstCommentLine] elif line != '': comment += [line.strip()] @@ -113,7 +113,7 @@ def readPdfBankStatement(filePath): 'date': getDateFollowing(date, initialDate), 'valeur': getDateFollowing(valeur, initialDate), 'amount': amount, - #'currentAmount': currentAmount, + 'current amount': currentAmount, 'comment': '\n'.join(comment) }] return initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime