"""Export XLSX des courses pour Tunis : une feuille claire, titres en majuscules sans accents.""" import io import zipfile from typing import List, Dict, Any from openpyxl import Workbook from openpyxl.styles import Alignment, Border, Font, PatternFill, Side from openpyxl.utils import get_column_letter AUTEUR = "Nabil.Derouiche" SENS_LIBELLE = {"tunis_sfax": "Tunis vers Sfax", "sfax_tunis": "Sfax vers Tunis"} STATUT_LIBELLE = {"a_faire": "A faire", "faite": "Faite"} ENTETES = ["N°", "DEMANDE", "SENS", "DATE DU MAIL", "EXPEDITEUR", "OBJET DU MAIL", "STATUT", "REALISEE LE"] LARGEURS = [6, 46, 17, 14, 30, 46, 11, 14] def _jj_mm_aaaa(iso: str) -> str: j = (iso or "")[:10] p = j.split("-") return f"{p[2]}/{p[1]}/{p[0]}" if len(p) == 3 and len(j) == 10 else "" def construire_xlsx(courses: List[Dict[str, Any]], libelle_filtre: str, aujourdhui_iso: str) -> bytes: wb = Workbook() ws = wb.active ws.title = "COURSES" wb.properties.creator = AUTEUR wb.properties.lastModifiedBy = AUTEUR wb.properties.title = "Courses pour Tunis" police = "Calibri" fin = Side(style="thin", color="B7C0CC") bord = Border(left=fin, right=fin, top=fin, bottom=fin) ws["A1"] = "COURSES POUR TUNIS" ws["A1"].font = Font(name=police, size=16, bold=True, color="1F3A5F") ws.merge_cells("A1:H1") ws["A2"] = f"Etat au {_jj_mm_aaaa(aujourdhui_iso)} - {libelle_filtre} - {len(courses)} ligne(s)" ws["A2"].font = Font(name=police, size=11, color="44546A") ws.merge_cells("A2:H2") for i, t in enumerate(ENTETES, 1): c = ws.cell(row=4, column=i, value=t) c.font = Font(name=police, size=11, bold=True, color="FFFFFF") c.fill = PatternFill("solid", fgColor="1F3A5F") c.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True) c.border = bord ws.row_dimensions[4].height = 24 for n, k in enumerate(courses, 1): ligne = [n, k.get("demande") or "", SENS_LIBELLE.get(k.get("sens"), ""), _jj_mm_aaaa(k.get("date_mail") or ""), k.get("expediteur") or "", k.get("objet_mail") or "", STATUT_LIBELLE.get(k.get("statut"), ""), _jj_mm_aaaa(k.get("fait_le") or "")] r = 4 + n for i, v in enumerate(ligne, 1): c = ws.cell(row=r, column=i, value=v) c.font = Font(name=police, size=11, bold=(i == 2), color=("5B6B7F" if k.get("statut") == "faite" else "1B2430")) c.alignment = Alignment(vertical="top", wrap_text=True, horizontal=("center" if i in (1, 4, 7, 8) else "left")) c.border = bord if k.get("statut") == "faite": c.fill = PatternFill("solid", fgColor="EEF3EE") for i, w in enumerate(LARGEURS, 1): ws.column_dimensions[get_column_letter(i)].width = w ws.freeze_panes = "A5" if courses: ws.auto_filter.ref = f"A4:H{4 + len(courses)}" ws.page_setup.orientation = "landscape" ws.page_setup.paperSize = ws.PAPERSIZE_A4 ws.page_setup.fitToWidth = 1 ws.page_setup.fitToHeight = 0 ws.sheet_properties.pageSetUpPr.fitToPage = True ws.print_title_rows = "4:4" buf = io.BytesIO() wb.save(buf) return _proprietes_neutres(buf.getvalue()) APP_XML = ('' '' 'Microsoft Excel') def _proprietes_neutres(brut: bytes) -> bytes: """Réécrit les propriétés applicatives du classeur : aucune trace de l'outil de génération.""" sortie = io.BytesIO() with zipfile.ZipFile(io.BytesIO(brut)) as src, zipfile.ZipFile(sortie, "w", zipfile.ZIP_DEFLATED) as dst: for item in src.infolist(): data = src.read(item.filename) if item.filename == "docProps/app.xml": data = APP_XML.encode("utf-8") dst.writestr(item, data) return sortie.getvalue()