diff --git a/CHANGELOG.md b/CHANGELOG.md index 22cf1d3..1e96872 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- Excel import reads `.xlsx` files with `openpyxl` directly; `pandas` is no longer a dependency (smaller install and Docker image). + +### Removed +- `.xls` upload. Those files could never be read (the `xlrd` engine was not installed); save them as `.xlsx`. + +### Fixed +- Excel import matches column headers by whole word, like the PDF import (a header such as "Login method" was taken as the entry column). + ## [2.0.0] - 2026-10-03 First release prepared for self-hosting by others: login, a simpler interface, English and Spanish, holidays for any country and Docker. diff --git a/app/routes/import_log.py b/app/routes/import_log.py index 385c511..0955a55 100644 --- a/app/routes/import_log.py +++ b/app/routes/import_log.py @@ -32,7 +32,7 @@ # Configure upload folder UPLOAD_FOLDER = os.path.join(os.getcwd(), "uploads") os.makedirs(UPLOAD_FOLDER, exist_ok=True) -ALLOWED_EXTENSIONS = ("pdf", "xlsx", "xls") +ALLOWED_EXTENSIONS = ("pdf", "xlsx") # overwrite: imported times replace the day's times; skip: days that already # have an entry are left alone. IMPORT_MODES = ("overwrite", "skip") diff --git a/app/services/importer/columns.py b/app/services/importer/columns.py new file mode 100644 index 0000000..45b138e --- /dev/null +++ b/app/services/importer/columns.py @@ -0,0 +1,22 @@ +"""Recognize the columns of an imported report from their headers.""" + +import re +from typing import Optional + +# Header words for each column, matched as whole words: a substring match on +# "in" / "out" would also hit headers like "Login method" or "Checkout notes". +COLUMN_WORDS = { + "date": {"fecha", "date", "day", "dia", "día"}, + "entry": {"entrada", "ingreso", "in", "entry", "start"}, + "exit": {"salida", "egreso", "out", "exit", "end"}, + "obs": {"observacion", "observación", "observaciones", "obs", "note", "notes"}, +} + + +def column_kind(header: str) -> Optional[str]: + """Return which column a header names ("date", "entry", ...), if any.""" + words = set(re.findall(r"\w+", header.lower())) + for kind, names in COLUMN_WORDS.items(): + if words & names: + return kind + return None diff --git a/app/services/importer/excel_importer.py b/app/services/importer/excel_importer.py index dbf0a57..e7300c3 100644 --- a/app/services/importer/excel_importer.py +++ b/app/services/importer/excel_importer.py @@ -1,8 +1,10 @@ import io -from typing import Any, List, Optional +from datetime import date, datetime, time +from typing import Any, Dict, List, Optional -import pandas as pd # type: ignore +from openpyxl import load_workbook # type: ignore +from app.services.importer.columns import column_kind from app.services.importer.protocol import ( ImporterProtocol, ImportResult, @@ -12,52 +14,43 @@ class ExcelImporter(ImporterProtocol): + """Reads .xlsx files: the first row holds the headers, one day per row.""" + def parse(self, file_content: Any) -> ImportResult: records: List[TimeEntryRecord] = [] errors: List[str] = [] try: - # Read Excel file - df = pd.read_excel(io.BytesIO(file_content)) - - # Normalize headers - df.columns = df.columns.astype(str).str.lower().str.strip() - - # Map columns - col_map = {} - for col in df.columns: - if "fecha" in col or "date" in col: - col_map["date"] = col - elif "entrada" in col or "in" in col: - col_map["entry"] = col - elif "salida" in col or "out" in col: - col_map["exit"] = col - elif "observ" in col or "note" in col: - col_map["obs"] = col + workbook = load_workbook( + io.BytesIO(file_content), read_only=True, data_only=True + ) + rows = workbook.active.iter_rows(values_only=True) + headers = next(rows, ()) + + # Map each kind of column to its index (first match wins). + col_map: Dict[str, int] = {} + for idx, header in enumerate(headers): + kind = column_kind(str(header)) if header is not None else None + if kind and kind not in col_map: + col_map[kind] = idx if "date" not in col_map: errors.append("Could not find 'Fecha' or 'Date' column") return ImportResult([], 0, 0, errors) - for _, row in df.iterrows(): - date_val = row[col_map["date"]] - if pd.isna(date_val): + for row in rows: + date_val = _cell(row, col_map.get("date")) + if _is_empty(date_val): continue - # Handle dates - if isinstance(date_val, pd.Timestamp): + if isinstance(date_val, (datetime, date)): date_str = date_val.strftime("%Y-%m-%d") else: date_str = str(date_val).strip() - entry_val = ( - row.get(col_map.get("entry")) if "entry" in col_map else None - ) - exit_val = row.get(col_map.get("exit")) if "exit" in col_map else None - obs_val = row.get(col_map.get("obs")) if "obs" in col_map else None - - entry_str = self._format_time(entry_val) - exit_str = self._format_time(exit_val) + entry_str = self._format_time(_cell(row, col_map.get("entry"))) + exit_str = self._format_time(_cell(row, col_map.get("exit"))) + obs_val = _cell(row, col_map.get("obs")) # Logic for validating is_valid = True @@ -78,12 +71,14 @@ def parse(self, file_content: Any) -> ImportResult: date=date_str, entry_time=entry_str, exit_time=exit_str, - observation=str(obs_val) if pd.notna(obs_val) else None, + observation=None if _is_empty(obs_val) else str(obs_val), is_valid=is_valid, error_message=error_msg, ) ) + workbook.close() + except Exception as e: errors.append(f"Error parsing Excel: {str(e)}") @@ -91,18 +86,20 @@ def parse(self, file_content: Any) -> ImportResult: return ImportResult(records, len(records), valid_records, errors) def _format_time(self, val: Any) -> Optional[str]: - if pd.isna(val): + if _is_empty(val): return None - if isinstance(val, pd.Timestamp): - return str(val.strftime("%H:%M")) + if isinstance(val, (datetime, time)): + return val.strftime("%H:%M") + + return str(val).strip() + + +def _cell(row: tuple, idx: Optional[int]) -> Any: + if idx is None or idx >= len(row): + return None + return row[idx] - # If it's a datetime.time object - try: - return str(val.strftime("%H:%M")) - except AttributeError: - pass - s = str(val).strip() - # Basic fixes - return s +def _is_empty(val: Any) -> bool: + return val is None or (isinstance(val, str) and not val.strip()) diff --git a/app/services/importer/factory.py b/app/services/importer/factory.py index ecb0c32..b6bbee9 100644 --- a/app/services/importer/factory.py +++ b/app/services/importer/factory.py @@ -9,7 +9,6 @@ class ImporterFactory: _importers: Dict[str, Type[ImporterProtocol]] = { "pdf": PDFImporter, "xlsx": ExcelImporter, - "xls": ExcelImporter, } @classmethod diff --git a/app/services/importer/pdf_importer.py b/app/services/importer/pdf_importer.py index dd418b0..69b23bd 100644 --- a/app/services/importer/pdf_importer.py +++ b/app/services/importer/pdf_importer.py @@ -4,6 +4,7 @@ import pdfplumber # type: ignore +from app.services.importer.columns import column_kind from app.services.importer.protocol import ( ImporterProtocol, ImportResult, @@ -11,24 +12,6 @@ ) from app.utils.validators import validate_date, validate_time_format -# Header words for each column, matched as whole words: a substring match on -# "in" / "out" would also hit headers like "Overtime" or "Checkout notes". -COLUMN_WORDS = { - "date": {"fecha", "date", "day", "dia", "día"}, - "entry": {"entrada", "ingreso", "in", "entry", "start"}, - "exit": {"salida", "egreso", "out", "exit", "end"}, - "obs": {"observacion", "observación", "observaciones", "obs", "note", "notes"}, -} - - -def _column_kind(header: str) -> Optional[str]: - """Return which column a header names ("date", "entry", ...), if any.""" - words = set(re.findall(r"\w+", header)) - for kind, names in COLUMN_WORDS.items(): - if words & names: - return kind - return None - class PDFImporter(ImporterProtocol): def parse(self, file_content: Any) -> ImportResult: @@ -64,7 +47,7 @@ def _process_table(self, table: List[List[Optional[str]]]) -> List[TimeEntryReco if "fecha" in row_clean or "date" in row_clean: # Map columns for col_idx, val in enumerate(row_clean): - kind = _column_kind(val) + kind = column_kind(val) if kind and kind not in header_map: header_map[kind] = col_idx data_start_idx = idx + 1 diff --git a/app/templates/settings.html b/app/templates/settings.html index 87a3de0..3610fbd 100644 --- a/app/templates/settings.html +++ b/app/templates/settings.html @@ -44,12 +44,12 @@

{{ _('Absence codes') }}

{{ _('Import hours') }}

-

{{ _('Upload a PDF or Excel report (xlsx, xls) with date, entry, exit and observation columns. You will see a preview before anything is saved.') }}

+

{{ _('Upload a PDF or Excel report (xlsx) with date, entry, exit and observation columns. You will see a preview before anything is saved.') }}

- +