This repository has been archived on 2026-06-28. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Dienstplan/src/read_excel.py

81 lines
2.1 KiB
Python

"""
Excel-Datei einlesen und Inhalt anzeigen
"""
from openpyxl import load_workbook
import json
from pathlib import Path
def read_excel_to_dict(filepath):
"""Liest eine Excel-Datei und gibt die Daten als Dictionary zurück."""
wb = load_workbook(filepath, data_only=True)
result = {}
for sheet_name in wb.sheetnames:
ws = wb[sheet_name]
# Daten aus dem Sheet lesen
data = []
for row in ws.iter_rows(values_only=True):
# Nur Zeilen mit Inhalt
if any(cell is not None for cell in row):
data.append(list(row))
result[sheet_name] = data
return result
def print_excel_content(filepath):
"""Gibt den Inhalt einer Excel-Datei formatiert aus."""
print(f"\n{'='*60}")
print(f"Excel-Datei: {filepath}")
print(f"{'='*60}\n")
data = read_excel_to_dict(filepath)
for sheet_name, rows in data.items():
print(f"\n📊 Sheet: {sheet_name}")
print(f"{'-'*60}")
if not rows:
print(" (leer)")
continue
# Tabelle ausgeben
for i, row in enumerate(rows, 1):
row_str = " | ".join(str(cell) if cell is not None else "" for cell in row)
print(f" {i:3d}: {row_str}")
print(f"\n{'='*60}\n")
# Als JSON ausgeben
print("📄 JSON-Format:")
print(json.dumps(data, indent=2, ensure_ascii=False))
return data
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
# Datei als Argument übergeben
filepath = sys.argv[1]
else:
# Nach neuester Datei im output-Ordner suchen
output_dir = Path("output")
excel_files = list(output_dir.glob("*.xlsx"))
if not excel_files:
print("❌ Keine Excel-Dateien im output-Ordner gefunden!")
print("Verwendung: python src/read_excel.py <pfad-zur-datei>")
sys.exit(1)
# Neueste Datei verwenden
filepath = max(excel_files, key=lambda p: p.stat().st_mtime)
print_excel_content(filepath)