add revolut
This commit is contained in:
@@ -5,7 +5,7 @@ import sys
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
# extract transactions from trade republic account statement (pdf or pdftotext -layout txt)
|
||||
# extract transactions from trade republic account statement pdf
|
||||
HEADER = re.compile(r"^\s*DATE\b.*\bMONEY IN\b.*\bMONEY OUT\b.*\bBALANCE\b")
|
||||
TEXT_COLUMNS = ("DATE", "TYPE", "DESCRIPTION")
|
||||
MONEY_COLUMNS = ("MONEY IN", "MONEY OUT", "BALANCE")
|
||||
@@ -16,14 +16,12 @@ IBAN = re.compile(r"\b[A-Z]{2}\d{2}[A-Z0-9]{11,30}\b")
|
||||
AMOUNT_SLACK = 5
|
||||
|
||||
|
||||
def read_text(path):
|
||||
if path.suffix.lower() == ".pdf":
|
||||
result = subprocess.run(
|
||||
["pdftotext", "-layout", str(path), "-"],
|
||||
capture_output=True, text=True, check=True,
|
||||
)
|
||||
return result.stdout
|
||||
return path.read_text(encoding="utf-8")
|
||||
def pdf_to_text(pdf_path):
|
||||
result = subprocess.run(
|
||||
["pdftotext", "-layout", str(pdf_path), "-"],
|
||||
capture_output=True, text=True, check=True,
|
||||
)
|
||||
return result.stdout
|
||||
|
||||
|
||||
def column_starts(header_line):
|
||||
@@ -110,7 +108,7 @@ def blocks(lines):
|
||||
def extract_transactions(path):
|
||||
print(path)
|
||||
rows = []
|
||||
for page_text in read_text(path).split("\f"):
|
||||
for page_text in pdf_to_text(path).split("\f"):
|
||||
page_lines = page_text.split("\n")
|
||||
header = next((i for i, l in enumerate(page_lines) if HEADER.match(l)), None)
|
||||
if header is None:
|
||||
@@ -134,25 +132,14 @@ def check_balances(rows, path):
|
||||
|
||||
def main():
|
||||
target = Path(sys.argv[1])
|
||||
if target.is_dir():
|
||||
paths = sorted(p for p in target.iterdir() if p.suffix.lower() in {".pdf", ".txt"})
|
||||
else:
|
||||
paths = [target]
|
||||
paths = sorted(target.glob("*.pdf")) if target.is_dir() else [target]
|
||||
out_path = target.with_suffix(".csv")
|
||||
|
||||
all_rows = []
|
||||
seen = set()
|
||||
duplicates = 0
|
||||
for path in paths:
|
||||
rows = extract_transactions(path)
|
||||
check_balances(rows, path)
|
||||
for row in rows:
|
||||
key = (row["date"], row["desc"], row["amount"], row["balance"])
|
||||
if key in seen:
|
||||
duplicates += 1
|
||||
continue
|
||||
seen.add(key)
|
||||
all_rows.append(row)
|
||||
all_rows.extend(rows)
|
||||
|
||||
with open(out_path, "w", newline="") as f:
|
||||
writer = csv.DictWriter(
|
||||
@@ -161,8 +148,6 @@ def main():
|
||||
writer.writeheader()
|
||||
writer.writerows(all_rows)
|
||||
|
||||
if duplicates:
|
||||
print(f"Skipped {duplicates} transactions already seen in an earlier statement")
|
||||
print(f"Wrote {len(all_rows)} transactions to {out_path}")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user