add revolut
This commit is contained in:
+24
-6
@@ -2,6 +2,7 @@ import csv
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
# extract transaction from izpisek pdf
|
||||
TRANSACTION_START = re.compile(r"^\d{2}\.\d{2}\.\d{2}\s")
|
||||
@@ -24,6 +25,14 @@ def pdf_to_text(pdf_path):
|
||||
return result.stdout
|
||||
|
||||
|
||||
def to_number(text):
|
||||
"""Statements write 1.234,56 and mark a negative balance with a trailing minus."""
|
||||
text = text.strip()
|
||||
if text.endswith("-"):
|
||||
text = "-" + text[:-1]
|
||||
return Decimal(text.replace(".", "").replace(",", "."))
|
||||
|
||||
|
||||
def parse_transaction(line):
|
||||
m = TRANSACTION_LINE.match(line.rstrip())
|
||||
account = ""
|
||||
@@ -33,15 +42,12 @@ def parse_transaction(line):
|
||||
account = f
|
||||
else:
|
||||
desc_parts.append(f)
|
||||
balance = m.group("balance")
|
||||
if balance.endswith("-"):
|
||||
balance = "-" + balance[:-1]
|
||||
return {
|
||||
"date": m.group("date"),
|
||||
"desc": " ".join(desc_parts),
|
||||
"account": account,
|
||||
"amount": m.group("amount"),
|
||||
"balance": balance,
|
||||
"amount": f"{to_number(m.group('amount')):+.2f}",
|
||||
"balance": f"{to_number(m.group('balance')):.2f}",
|
||||
"currency": "EUR",
|
||||
}
|
||||
|
||||
@@ -70,13 +76,25 @@ def extract_transactions(pdf_path):
|
||||
return rows
|
||||
|
||||
|
||||
def check_balances(rows, path):
|
||||
"""Every balance must be the previous one plus the transaction amount."""
|
||||
for prev, row in zip(rows, rows[1:]):
|
||||
expected = Decimal(prev["balance"]) + Decimal(row["amount"])
|
||||
if expected != Decimal(row["balance"]):
|
||||
print(f" warning: {path.name} {row['date']} {row['desc'][:40]!r} "
|
||||
f"balance {row['balance']} != {expected}")
|
||||
|
||||
|
||||
def main():
|
||||
pdf_dir = Path(sys.argv[1])
|
||||
out_path = pdf_dir.with_suffix(".csv")
|
||||
|
||||
all_rows = []
|
||||
for pdf_path in sorted(pdf_dir.glob("*.pdf")):
|
||||
all_rows.extend(extract_transactions(pdf_path))
|
||||
rows = extract_transactions(pdf_path)
|
||||
# consecutive izpiski of one account, so the chain carries over the file boundary
|
||||
check_balances(all_rows[-1:] + rows, pdf_path)
|
||||
all_rows.extend(rows)
|
||||
|
||||
with open(out_path, "w", newline="") as f:
|
||||
writer = csv.DictWriter(f, fieldnames=["date", "desc", "account", "amount", "balance", "currency"])
|
||||
|
||||
Reference in New Issue
Block a user