7 from collections import namedtuple
8 from pathlib import Path
9 from typing import Iterable
11 Entry = namedtuple('Entry', ['category', 'date', 'valuta', 'who', 'iban', 'bic', 'type', 'desc', 'value'])
14 'Gehalt/Steuern': re.compile('Gehalt/Rente|RED HAT|Finanzamt|FK Guenzburg', re.I),
15 'Wohnung': re.compile('Telekom|hands4home|Goetzfried|Green Planet Energy|Beitragsservice.*Rundfunk', re.I),
16 'Medizin': re.compile(r'Apotheke|MVZ|\bmed\b|ZAB Abrechnung|Caika|HNOeins|PVS|Dr\..*Sellier|'
17 r'BFS Health|Streifeneder|Labor|Physio|(Drescher.*Lung)|'
18 '(Dr.*Borchers)|(Debeka.*Überweisung)|(DKV.*Überweisung)|Beihilfe', re.I),
19 'Versicherung': re.compile('(debeka|DKV|Hallesche|Versicherung|Alte Leipziger|ConceptIF|'
20 'Baloise).*Lastschrift', re.I),
21 'Transport': re.compile('DB Vertrieb|Deutsche Bahn|Nextbike|Carsharing', re.I),
22 'Lebensmittel': re.compile('BIOS|Wolf|Ruta|Rewe', re.I),
23 'Eigentumswohnungen': re.compile('Rechnung Darl.-Leistung|Semmelweis', re.I),
24 'Hobby/Sport': re.compile('Holstein|Mrs. Sporty|Kieser|DJK', re.I),
25 'Sparen': re.compile('Bausparkasse Mainz AG|FIL Fondsbank|MIG (Fonds|GmbH)|Netfonds AG', re.I),
26 'Spenden': re.compile('Spende|Signal Foundation|nebenan.de|(paypal.*Sabine.Hossenf)|'
27 'Patreon|Umwelthilfe|Foerderer|Malteser|(Aktion Tier.*Mensch)|'
28 'campact|Amnesty', re.I),
32 def parse_args() -> argparse.Namespace:
33 parser = argparse.ArgumentParser(description='Read Consorsbank CSV and generate report')
34 parser.add_argument('-d', '--date', metavar='REGEX', default='.', help='date filter regex')
35 parser.add_argument('-s', '--summary', action='store_true', help='only show category sums')
36 parser.add_argument('csv', type=Path, nargs='+', help='CSV files to parse')
37 return parser.parse_args()
40 def get_category(item: str) -> str:
41 for category, pattern in CATEGORIES.items():
42 if pattern.search(item):
47 def parse_entry(line: str) -> Entry:
48 fields = [f.strip() for f in line.strip().split(';')]
49 # last field is the value, parse as float
50 value = float(fields.pop().replace('.', '').replace(',', '.'))
51 # match on who, IBAN, type, or desc
52 category = get_category(fields[2] + fields[3] + fields[5] + fields[6])
53 return Entry._make([category, *fields, value])
56 def parse_csv(path: Path, date_filter: str) -> Iterable[Entry]:
57 filter_re = re.compile(date_filter, re.I)
59 def filter_entry(entry: Entry):
60 # ignore from/to Tagesgeldkonto
61 if entry.iban == 'DE13760300800853589101':
63 return filter_re.search(entry.date)
65 with path.open() as f:
66 # first line is the column headers, chop it off
67 entries = map(parse_entry, f.readlines()[1:])
68 return filter(filter_entry, entries)
74 entries = itertools.chain.from_iterable(map(lambda p: parse_csv(p, args.date), args.csv))
75 by_category = collections.defaultdict(lambda: [])
76 category_sum = collections.defaultdict(lambda: 0.0)
79 by_category[e.category].append(e)
80 category_sum[e.category] += e.value
84 for cat in sorted(by_category):
85 print(f'| {category_sum[cat]:>8.2f} | {cat:20} |')
86 print('|----------|----------------------|')
87 print(f'| {balance:>8.2f} | {"Saldo":20} |')
90 for cat in sorted(by_category):
91 print(f'## {cat}: {category_sum[cat]:.2f}\n')
92 for entry in by_category[cat]:
93 print(f'| {entry.date:7} | {entry.value:>7.2f} | _{entry.type}_ - {entry.who} - {entry.desc} |')
96 print(f'## Saldo: {balance:.2f}')
99 if __name__ == '__main__':