Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion app/routes/import_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
22 changes: 22 additions & 0 deletions app/services/importer/columns.py
Original file line number Diff line number Diff line change
@@ -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
85 changes: 41 additions & 44 deletions app/services/importer/excel_importer.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -78,31 +71,35 @@ 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)}")

valid_records = sum(1 for r in records if r.is_valid)
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())
1 change: 0 additions & 1 deletion app/services/importer/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class ImporterFactory:
_importers: Dict[str, Type[ImporterProtocol]] = {
"pdf": PDFImporter,
"xlsx": ExcelImporter,
"xls": ExcelImporter,
}

@classmethod
Expand Down
21 changes: 2 additions & 19 deletions app/services/importer/pdf_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,14 @@

import pdfplumber # type: ignore

from app.services.importer.columns import column_kind
from app.services.importer.protocol import (
ImporterProtocol,
ImportResult,
TimeEntryRecord,
)
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:
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ <h2 class="h5" id="codesTitle">{{ _('Absence codes') }}</h2>
<section class="card h-100" id="import" aria-labelledby="importTitle">
<div class="card-body">
<h2 class="h5" id="importTitle">{{ _('Import hours') }}</h2>
<p class="small text-body-secondary">{{ _('Upload a PDF or Excel report (xlsx, xls) with date, entry, exit and observation columns. You will see a preview before anything is saved.') }}</p>
<p class="small text-body-secondary">{{ _('Upload a PDF or Excel report (xlsx) with date, entry, exit and observation columns. You will see a preview before anything is saved.') }}</p>
<form method="POST" action="{{ url_for('import_log.upload_file') }}" enctype="multipart/form-data">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<div class="mb-3">
<label for="file" class="form-label">{{ _('File') }}</label>
<input class="form-control" type="file" id="file" name="file" accept=".pdf,.xlsx,.xls" required>
<input class="form-control" type="file" id="file" name="file" accept=".pdf,.xlsx" required>
</div>
<button type="submit" class="btn btn-primary">
{{ icon('upload', 'me-1') }} {{ _('Upload and preview') }}
Expand Down
Binary file modified app/translations/es/LC_MESSAGES/messages.mo
Binary file not shown.
10 changes: 5 additions & 5 deletions app/translations/es/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2026-10-03 01:51-0300\n"
"POT-Creation-Date: 2026-10-03 06:28-0300\n"
"PO-Revision-Date: 2026-10-03 01:51-0300\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: es\n"
Expand Down Expand Up @@ -413,11 +413,11 @@ msgid "Unsupported file type"
msgstr "Tipo de archivo no soportado"

msgid ""
"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."
msgstr ""
"Subí un reporte en PDF o Excel (xlsx, xls) con columnas de fecha, entrada, salida y observación. "
"Vas a ver una vista previa antes de que se guarde nada."
"Subí un reporte en PDF o Excel (xlsx) con columnas de fecha, entrada, salida y observación. Vas a"
" ver una vista previa antes de que se guarde nada."

msgid "Upload and preview"
msgstr "Subir y previsualizar"
Expand Down
4 changes: 2 additions & 2 deletions app/translations/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2026-10-03 01:51-0300\n"
"POT-Creation-Date: 2026-10-03 06:28-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -410,7 +410,7 @@ msgid "Unsupported file type"
msgstr ""

msgid ""
"Upload a PDF or Excel report (xlsx, xls) with date, entry, exit and "
"Upload a PDF or Excel report (xlsx) with date, entry, exit and "
"observation columns. You will see a preview before anything is saved."
msgstr ""

Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ pytest-mock==3.12.0
# Type stubs
types-requests==2.31.0.20240125
types-Flask-Migrate==4.0.0
pandas-stubs==2.2.0.240218

1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ psycopg2-binary==2.9.10
python-dotenv==1.0.1
beautifulsoup4==4.13.4

pandas==2.2.0
pdfplumber==0.11.0
openpyxl==3.1.2
gunicorn==23.0.0
Loading
Loading