fix changed month sheet name (Mär -> Mrz)

This commit is contained in:
Rob committed 2026-05-19 15:34:17 +02:00
1 parent d07b4832c6
commit 7c629f189a
2 files changed
+34 -5

No files matched your search

+26
View File
@@ -139,6 +139,30 @@ def get_sheet_names(file_path: str | Path) -> List[str]:
return names return names
def resolve_sheet_name(
sheet_name: str,
workbook_sheets: List[str],
month_mapping: Dict[str, str],
) -> str:
"""Resolve a month alias to the actual sheet name present in the workbook.
If *sheet_name* exists in *workbook_sheets*, it is returned unchanged.
Otherwise all aliases in *month_mapping* that map to the same month number
are checked, and the first one present in *workbook_sheets* is returned.
If no alias matches, the original *sheet_name* is returned so that the
normal missing-sheet error can fire.
"""
if sheet_name in workbook_sheets:
return sheet_name
target_month = month_mapping.get(sheet_name)
if target_month is None:
return sheet_name
for alias, month_num in month_mapping.items():
if month_num == target_month and alias in workbook_sheets:
return alias
return sheet_name
def extract_persons( def extract_persons(
file_path: str | Path, file_path: str | Path,
sheet_name: str, sheet_name: str,
@@ -158,6 +182,7 @@ def extract_persons(
wb = _open_workbook(Path(file_path)) wb = _open_workbook(Path(file_path))
try: try:
sheet_name = resolve_sheet_name(sheet_name, wb.sheetnames, config["month_mapping"])
data = _sheet_data(wb, sheet_name) data = _sheet_data(wb, sheet_name)
finally: finally:
wb.close() wb.close()
@@ -217,6 +242,7 @@ def extract_attendance(
wb = _open_workbook(Path(file_path)) wb = _open_workbook(Path(file_path))
try: try:
sheet_name = resolve_sheet_name(sheet_name, wb.sheetnames, month_mapping)
data = _sheet_data(wb, sheet_name) data = _sheet_data(wb, sheet_name)
finally: finally:
wb.close() wb.close()
+8 -5
View File
@@ -14,6 +14,7 @@ from pathlib import Path
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from .config_loader import load_config from .config_loader import load_config
from .excel_reader import resolve_sheet_name
from .exceptions import ConfigurationError from .exceptions import ConfigurationError
@@ -71,18 +72,19 @@ def validate(
) )
sheet_names = wb.sheetnames sheet_names = wb.sheetnames
# Check 4: sheet name valid # Check 4: sheet name valid (resolve alias, e.g. Mär → Mrz)
if sheet_name not in config["month_mapping"]: if sheet_name not in config["month_mapping"]:
errors.append( errors.append(
f"Sheet-Name '{sheet_name}' nicht in month_mapping der config.json." f"Sheet-Name '{sheet_name}' nicht in month_mapping der config.json."
) )
if sheet_name not in sheet_names: resolved = resolve_sheet_name(sheet_name, sheet_names, config["month_mapping"])
if resolved not in sheet_names:
errors.append( errors.append(
f"Sheet '{sheet_name}' existiert nicht in der Arbeitsmappe. " f"Sheet '{sheet_name}' existiert nicht in der Arbeitsmappe. "
f"Verfügbar: {sheet_names}" f"Verfügbar: {sheet_names}"
) )
else: else:
ws = wb[sheet_name] ws = wb[resolved]
rows = list(ws.iter_rows(values_only=True)) rows = list(ws.iter_rows(values_only=True))
name_col: int = config["excel_name_column"] name_col: int = config["excel_name_column"]
@@ -179,11 +181,12 @@ def _extract_samples(
try: try:
wb = openpyxl.load_workbook(str(file_path), read_only=True, data_only=True) wb = openpyxl.load_workbook(str(file_path), read_only=True, data_only=True)
if sheet_name not in wb.sheetnames: resolved = resolve_sheet_name(sheet_name, wb.sheetnames, config["month_mapping"])
if resolved not in wb.sheetnames:
wb.close() wb.close()
return result return result
ws = wb[sheet_name] ws = wb[resolved]
rows = list(ws.iter_rows(values_only=True)) rows = list(ws.iter_rows(values_only=True))
wb.close() wb.close()