#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Report: DXA requisitions (Medicus → Dropbox) -------------------------------------------- - Selects all histdoc records for year 2025 containing "DXA" - Finds matching PDF files in Dropbox (by rod_cis + "dxa" in name) - Outputs Excel report: datum, idpaci, rod_cis, prijmeni, jmeno, file """ import os import firebirdsql as fb import pandas as pd from pathlib import Path from datetime import datetime from Functions import get_dropbox_path from Functions import get_medicus_connection # ================== CONFIGURATION ================== EXPORT_DIR = Path(get_dropbox_path("reporty")) timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") xlsx_path = EXPORT_DIR / f"{timestamp}_Nové_registrace.xlsx" # ================== DATABASE QUERY ================== conn = get_medicus_connection() sql = """ select rodcis, prijmeni, jmeno, datum_registrace, registr.idpac, poj from registr join kar on registr.idpac=kar.idpac where kar.vyrazen!='A' and kar.rodcis is not null and idicp!=0 and datum_zruseni is null """ df = pd.read_sql(sql, conn) conn.close() # ================== CLEAN OLD REPORTS ================== for f in EXPORT_DIR.glob("*_Nové_registrace.xlsx"): try: f.unlink() print(f"🗑️ Deleted old report: {f.name}") except Exception as e: print(f"⚠️ Could not delete {f.name}: {e}") # ================== EXPORT TO EXCEL ================== with pd.ExcelWriter(xlsx_path, engine="openpyxl") as writer: df.to_excel(writer, index=False, sheet_name="Registrace 2025") ws = writer.sheets["Registrace 2025"] # Format header from openpyxl.styles import Font, Alignment, PatternFill, Border, Side header_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid") for cell in ws[1]: cell.font = Font(bold=True, color="000000") cell.alignment = Alignment(horizontal="center", vertical="center") cell.fill = header_fill # Auto column width, but hardcode FILE column to 120 from openpyxl.utils import get_column_letter for col in ws.columns: col_letter = get_column_letter(col[0].column) header = ws.cell(row=1, column=col[0].column).value if header == "FILE": ws.column_dimensions[col_letter].width = 120 else: max_len = max(len(str(cell.value)) if cell.value else 0 for cell in col) ws.column_dimensions[col_letter].width = min(max_len + 2, 80) # Borders thin = Side(border_style="thin", color="000000") border = Border(top=thin, left=thin, right=thin, bottom=thin) for row in ws.iter_rows(min_row=1, max_row=ws.max_row, min_col=1, max_col=ws.max_column): for cell in row: cell.border = border print(f"✅ Report created: {xlsx_path}") print(f"🩻 {df['FILE'].astype(bool).sum()} matching DXA PDFs found")