This commit is contained in:
2025-10-24 18:55:29 +02:00
parent 45ecfe96e8
commit c94d3b9e24
2 changed files with 20 additions and 21 deletions

View File

@@ -20,26 +20,20 @@ from pathlib import Path
from striprtf.striprtf import rtf_to_text
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
from openpyxl.utils import get_column_letter
from Functions import get_medicus_connection
# ================== CONFIGURATION ==================
FDB_PATH = r"z:\Medicus 3\data\MEDICUS.FDB"
EXPORT_DIR = Path(r"u:\Dropbox\!!!Days\Downloads Z230")
# calculate last 2 months dynamically
# calculate last 2 months dynamically (now set to 10 days for testing)
DATE_FROM = (datetime.now() - timedelta(days=10)).strftime("%Y-%m-%d")
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
xlsx_path = EXPORT_DIR / f"{timestamp}_Dekurz (poslední rok).xlsx"
# ================== FIREBIRD CONNECTION ==================
conn = fb.connect(
host="192.168.1.4",
port=3050,
database=FDB_PATH,
user="SYSDBA",
password="masterkey",
charset="WIN1250",
)
conn = get_medicus_connection()
def query_df(sql, params=None):
cur = conn.cursor()
@@ -85,21 +79,29 @@ def safe_rtf_to_text(x):
df["DEKURS"] = df["DEKURS"].apply(safe_rtf_to_text)
df.replace({r'(\r\n|\r|\n)': r'\r\n'}, regex=True, inplace=True)
df.replace({r'[\ud800-\udfff\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]+': ''},
regex=True, inplace=True)
df.replace({r'(\r\n){2,}': r'\r\n', r'(\r\n)+$': ''},
regex=True, inplace=True)
# --- Normalize and clean newlines ---
df["DEKURS"] = (
df["DEKURS"]
.replace(r"(\r\n|\r|\n)+", "\n", regex=True) # unify newlines
.replace(r"\n{2,}", "\n", regex=True) # collapse multiple blank lines
.str.strip() # trim leading/trailing blanks
)
# --- Remove invalid control characters ---
df.replace({r"[\ud800-\udfff\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]+": ""}, regex=True, inplace=True)
# --- Merge patient name ---
df["PACIENT"] = df["PRIJMENI"].fillna("") + ", " + df["JMENO"].fillna("")
df.drop(columns=["PRIJMENI", "JMENO"], inplace=True)
# --- Rename and format columns ---
df.rename(columns={"ZKRATKA": "LEKAR", "VYKONY_DNE": "VYKONY DNE"}, inplace=True)
df["DATUM"] = pd.to_datetime(df["DATUM"], errors="coerce").dt.date
df.drop(columns=[c for c in df.columns if "ASCII" in c.upper()], inplace=True, errors="ignore")
desired_order = ["DATUM", "RODCIS", "PACIENT", "LEKAR", "VYKONY DNE", "DEKURS"]
df = df[[c for c in desired_order if c in df.columns]]
# ================== CLEANUP OLD FILES ==================
for old_file in EXPORT_DIR.glob("*Dekurz (poslední rok)*.xlsx"):
try:
@@ -107,11 +109,12 @@ for old_file in EXPORT_DIR.glob("*Dekurz (poslední rok)*.xlsx"):
print(f"🧹 Deleted old file: {old_file.name}")
except Exception as e:
print(f"⚠️ Could not delete {old_file.name}: {e}")
# ================== EXPORT TO EXCEL ==================
with pd.ExcelWriter(xlsx_path, engine="openpyxl") as writer:
df.to_excel(writer, index=False, sheet_name="Dekurz")
ws = writer.sheets["Dekurz"]
ws.freeze_panes = "F2" #zamčení prvního řádku a sloupců A:F
# ----- Bright yellow header -----
header_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
for cell in ws[1]: