diff --git a/categorize_transactions.py b/categorize_transactions.py index d0d34d5..76cbf64 100644 --- a/categorize_transactions.py +++ b/categorize_transactions.py @@ -11,6 +11,7 @@ import subprocess from shutil import which, copy2 import platform import logging +from openpyxl.styles import Font # -------------------------------------------------- # Logging @@ -270,7 +271,9 @@ if sheet_name in wb.sheetnames: ws = wb.create_sheet(sheet_name) +# -------------------------------------------------- # Kennzahlen +# -------------------------------------------------- total_income = sum( values["income"] @@ -295,6 +298,10 @@ ws.append(["Saldo", total_saldo]) ws.append([]) +# -------------------------------------------------- +# Kategorien +# -------------------------------------------------- + ws.append([ "Kategorie", "Einnahmen", @@ -311,6 +318,61 @@ for category, values in sorted(category_totals.items()): values["saldo"] ]) +ws.append([]) +ws.append([]) + +# -------------------------------------------------- +# Kontobewegungen +# -------------------------------------------------- + +ws.append(["Kontobewegungen"]) + +ws.append([ + "Datum", + "Betrag", + "Kategorie", + "Name", + "Verwendungszweck" +]) + +for transaction in categorized_transactions: + + ws.append([ + transaction["date"], + transaction["amount"], + transaction["category"], + transaction["applicant_name"], + transaction["purpose"] + ]) + +# -------------------------------------------------- +# Formatierung KW-Blatt +# -------------------------------------------------- + +# Kennzahlen-Überschrift +for cell in ws[1]: + cell.font = Font(bold=True) + +# Kategorien-Überschrift suchen +for row in ws.iter_rows(): + + if row[0].value == "Kategorie": + + for cell in row: + cell.font = Font(bold=True) + + break + +# Kontobewegungen-Überschrift suchen +for row in ws.iter_rows(): + + if row[0].value == "Datum": + + for cell in row: + cell.font = Font(bold=True) + + break + # -------------------------------------------------- # Übersicht aktualisieren # -------------------------------------------------- @@ -327,6 +389,10 @@ overview.append([ "Saldo" ]) +# Überschrift fett +for cell in overview[1]: + cell.font = Font(bold=True) + for sheet in sorted( [s for s in wb.sheetnames if s.startswith("KW")] ): @@ -344,6 +410,39 @@ for sheet in sorted( saldo ]) +# -------------------------------------------------- +# Spaltenbreiten automatisch anpassen +# -------------------------------------------------- + +for worksheet in wb.worksheets: + + for column in worksheet.columns: + + max_length = 0 + column_letter = column[0].column_letter + + for cell in column: + + try: + + if cell.value: + + max_length = max( + max_length, + len(str(cell.value)) + ) + + except Exception: + pass + + worksheet.column_dimensions[ + column_letter + ].width = min(max_length + 2, 60) + +# -------------------------------------------------- +# Excel speichern +# -------------------------------------------------- + wb.save(excel_file) # --------------------------------------------------