diff --git a/categorize_transactions.py b/categorize_transactions.py index dee4a6c..a5567ba 100644 --- a/categorize_transactions.py +++ b/categorize_transactions.py @@ -5,6 +5,7 @@ from datetime import datetime import json import csv import sys +from openpyxl import Workbook # -------------------------------------------------- # Kalenderwoche bestimmen @@ -48,10 +49,12 @@ source_json = ( categorized_csv_dir = year_dir / "categorized_csv" categorized_json_dir = year_dir / "categorized_json" summary_dir = year_dir / "summary" +excel_dir = year_dir / "excel" categorized_csv_dir.mkdir(parents=True, exist_ok=True) categorized_json_dir.mkdir(parents=True, exist_ok=True) summary_dir.mkdir(parents=True, exist_ok=True) +excel_dir.mkdir(parents=True, exist_ok=True) csv_file = ( categorized_csv_dir @@ -68,6 +71,11 @@ summary_file = ( / f"category_summary_{year}_KW{week:02d}.json" ) +excel_file = ( + excel_dir + / f"transactions_{year}_KW{week:02d}.xlsx" +) + # -------------------------------------------------- # Prüfen ob Quelldatei existiert # -------------------------------------------------- @@ -202,6 +210,53 @@ with open(json_file, "w", encoding="utf-8") as outfile: indent=2 ) +# -------------------------------------------------- +# Excel exportieren +# -------------------------------------------------- + + +wb = Workbook() + +ws1 = wb.active +ws1.title = "Transaktionen" + +ws1.append([ + "Datum", + "Betrag", + "Kategorie", + "Name", + "Verwendungszweck" +]) + +for t in categorized_transactions: + ws1.append([ + t["date"], + t["amount"], + t["category"], + t["applicant_name"], + t["purpose"] + ]) + +ws2 = wb.create_sheet("Kategorien") + +ws2.append([ + "Kategorie", + "Einnahmen", + "Ausgaben", + "Saldo" +]) + +for category, values in sorted(category_totals.items()): + ws2.append([ + category, + values["income"], + values["expenses"], + values["saldo"] + ]) + +wb.save(excel_file) + + # -------------------------------------------------- # Kategorien-Summen exportieren # -------------------------------------------------- @@ -239,4 +294,5 @@ for category, values in sorted(category_totals.items()): print() print(f"CSV : {csv_file}") print(f"JSON : {json_file}") -print(f"Summary : {summary_file}") \ No newline at end of file +print(f"Summary : {summary_file}") +print(f"Excel : {excel_file}") \ No newline at end of file