import openpyxl from datetime import datetime datei = "Kopie von Kostenrechnung der Nächsten jahre (3).xlsx" print(f"Lade {datei}...") wb = openpyxl.load_workbook(datei, data_only=True) ws = wb["Tilgung bei Gleichbleibenden Be"] # Struktur: Kredite sind nebeneinander, jeweils 3-4 Spalten # Zeile 1: Kreditnamen # Zeile 2: Headers (Restschuld, Tilgung, Zinsen) # Zeile 3+: Monatliche Daten # Lese Zeile 1 um Kreditnamen zu finden print("\n=== KREDITE IM SHEET ===") row1 = list(ws.iter_rows(min_row=1, max_row=1, values_only=True))[0] kredite = {} for col_idx, cell in enumerate(row1, 1): if cell and isinstance(cell, str) and cell.strip(): name = cell.strip() # Finde Header für diesen Kredit (Zeile 2) headers = list(ws.iter_rows(min_row=2, max_row=2, values_only=True))[0] # Spalten für diesen Kredit restschuld_col = None tilgung_col = None zinsen_col = None # Schaue die nächsten 3-4 Spalten für Headers for h_idx in range(col_idx-1, min(col_idx+3, len(headers))): if headers[h_idx] == "Restschuld": restschuld_col = h_idx + 1 # 1-based elif headers[h_idx] == "Tilgung": tilgung_col = h_idx + 1 elif headers[h_idx] == "Zinsen": zinsen_col = h_idx + 1 if restschuld_col: kredite[name] = { "restschuld_col": restschuld_col, "tilgung_col": tilgung_col, "zinsen_col": zinsen_col, "start_col": col_idx } print(f" {name}: Restschuld in Spalte {restschuld_col}") print(f"\nInsgesamt {len(kredite)} Kredite gefunden\n") # Analysiere jeden Kredit print("="*80) print("ANALYSE JEDES KREDITS - Aktuelle Restschuld") print("="*80) aktive_kredite = [] abgezahlte_kredite = [] for name, cols in kredite.items(): rest_col = cols["restschuld_col"] tilg_col = cols["tilgung_col"] # Suche letzten Eintrag mit Restschuld letzte_restschuld = None letztes_datum = None start_datum = None monatsrate = None zinssatz = None for row_idx in range(3, ws.max_row + 1): datum_cell = ws.cell(row=row_idx, column=1).value rest_cell = ws.cell(row=row_idx, column=rest_col).value tilg_cell = ws.cell(row=row_idx, column=tilg_col).value if tilg_col else None # Startdatum = erstes Datum if start_datum is None and datum_cell and isinstance(datum_cell, datetime): start_datum = datum_cell # Monatsrate aus Tilgung-Spalte (wenn konstant) if tilg_cell and isinstance(tilg_cell, (int, float)) and tilg_cell > 0: if monatsrate is None: monatsrate = tilg_cell if rest_cell is not None and isinstance(rest_cell, (int, float)): if rest_cell > 0 or letzte_restschuld is None: letzte_restschuld = rest_cell letztes_datum = datum_cell # Berechne Zinssatz aus den Daten if letzte_restschuld is not None and monatsrate: # Versuche Zinssatz zu extrahieren # Aus historischen Daten berechnen for row_idx in range(3, min(30, ws.max_row)): rest = ws.cell(row=row_idx, column=rest_col).value zins = ws.cell(row=row_idx, column=cols.get("zinsen_col", 1)).value if cols.get("zinsen_col") else None if rest and zins and isinstance(rest, (int, float)) and isinstance(zins, (int, float)): if rest > 0 and zins > 0: # Monatlicher Zinssatz = Zinsen / Restschuld monatlicher_zins = zins / rest jaehrlicher_zins = monatlicher_zins * 12 * 100 zinssatz = round(jaehrlicher_zins, 2) break status = "ABGEZAHLT" if letzte_restschuld == 0 or letzte_restschuld is None else "AKTIV" kredit_info = { "name": name, "restschuld": letzte_restschuld if letzte_restschuld else 0, "start_datum": start_datum.strftime("%Y-%m-%d") if start_datum else None, "monatsrate": monatsrate, "zinssatz": zinssatz, "letztes_datum": letztes_datum.strftime("%Y-%m-%d") if letztes_datum else None } if status == "AKTIV": aktive_kredite.append(kredit_info) else: abgezahlte_kredite.append(kredit_info) print(f"\n{name}:") print(f" Status: {status}") print(f" Aktuelle Restschuld: {letzte_restschuld:,.2f} EUR" if letzte_restschuld else " Aktuelle Restschuld: 0,00 EUR") print(f" Startdatum: {start_datum.strftime('%d.%m.%Y') if start_datum else 'N/A'}") print(f" Monatsrate: {monatsrate:,.2f} EUR" if monatsrate else " Monatsrate: N/A") print(f" Geschätzter Zinssatz: {zinssatz}%" if zinssatz else " Zinssatz: N/A") print(f" Letzter Eintrag: {letztes_datum.strftime('%d.%m.%Y') if letztes_datum else 'N/A'}") print("\n" + "="*80) print("ZUSAMMENFASSUNG") print("="*80) print(f"\n🟢 AKTIVE KREDITE ({len(aktive_kredite)}):") for k in aktive_kredite: print(f" - {k['name']}: {k['restschuld']:,.2f} EUR") print(f"\n⚫ ABGEZAHLT ({len(abgezahlte_kredite)}):") for k in abgezahlte_kredite: print(f" - {k['name']}") print("\n" + "="*80) print("JSON für Import:") print("="*80) import json print(json.dumps(aktive_kredite, indent=2, default=str))