notebook
This commit is contained in:
33
50 Dekurs.py
33
50 Dekurs.py
@@ -20,26 +20,20 @@ from pathlib import Path
|
|||||||
from striprtf.striprtf import rtf_to_text
|
from striprtf.striprtf import rtf_to_text
|
||||||
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
|
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
|
||||||
from openpyxl.utils import get_column_letter
|
from openpyxl.utils import get_column_letter
|
||||||
|
from Functions import get_medicus_connection
|
||||||
|
|
||||||
# ================== CONFIGURATION ==================
|
# ================== CONFIGURATION ==================
|
||||||
FDB_PATH = r"z:\Medicus 3\data\MEDICUS.FDB"
|
FDB_PATH = r"z:\Medicus 3\data\MEDICUS.FDB"
|
||||||
EXPORT_DIR = Path(r"u:\Dropbox\!!!Days\Downloads Z230")
|
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")
|
DATE_FROM = (datetime.now() - timedelta(days=10)).strftime("%Y-%m-%d")
|
||||||
|
|
||||||
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
xlsx_path = EXPORT_DIR / f"{timestamp}_Dekurz (poslední rok).xlsx"
|
xlsx_path = EXPORT_DIR / f"{timestamp}_Dekurz (poslední rok).xlsx"
|
||||||
|
|
||||||
# ================== FIREBIRD CONNECTION ==================
|
# ================== FIREBIRD CONNECTION ==================
|
||||||
conn = fb.connect(
|
conn = get_medicus_connection()
|
||||||
host="192.168.1.4",
|
|
||||||
port=3050,
|
|
||||||
database=FDB_PATH,
|
|
||||||
user="SYSDBA",
|
|
||||||
password="masterkey",
|
|
||||||
charset="WIN1250",
|
|
||||||
)
|
|
||||||
|
|
||||||
def query_df(sql, params=None):
|
def query_df(sql, params=None):
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
@@ -85,21 +79,29 @@ def safe_rtf_to_text(x):
|
|||||||
|
|
||||||
df["DEKURS"] = df["DEKURS"].apply(safe_rtf_to_text)
|
df["DEKURS"] = df["DEKURS"].apply(safe_rtf_to_text)
|
||||||
|
|
||||||
df.replace({r'(\r\n|\r|\n)': r'\r\n'}, regex=True, inplace=True)
|
# --- Normalize and clean newlines ---
|
||||||
df.replace({r'[\ud800-\udfff\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]+': ''},
|
df["DEKURS"] = (
|
||||||
regex=True, inplace=True)
|
df["DEKURS"]
|
||||||
df.replace({r'(\r\n){2,}': r'\r\n', r'(\r\n)+$': ''},
|
.replace(r"(\r\n|\r|\n)+", "\n", regex=True) # unify newlines
|
||||||
regex=True, inplace=True)
|
.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["PACIENT"] = df["PRIJMENI"].fillna("") + ", " + df["JMENO"].fillna("")
|
||||||
df.drop(columns=["PRIJMENI", "JMENO"], inplace=True)
|
df.drop(columns=["PRIJMENI", "JMENO"], inplace=True)
|
||||||
|
|
||||||
|
# --- Rename and format columns ---
|
||||||
df.rename(columns={"ZKRATKA": "LEKAR", "VYKONY_DNE": "VYKONY DNE"}, inplace=True)
|
df.rename(columns={"ZKRATKA": "LEKAR", "VYKONY_DNE": "VYKONY DNE"}, inplace=True)
|
||||||
df["DATUM"] = pd.to_datetime(df["DATUM"], errors="coerce").dt.date
|
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")
|
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"]
|
desired_order = ["DATUM", "RODCIS", "PACIENT", "LEKAR", "VYKONY DNE", "DEKURS"]
|
||||||
df = df[[c for c in desired_order if c in df.columns]]
|
df = df[[c for c in desired_order if c in df.columns]]
|
||||||
|
|
||||||
# ================== CLEANUP OLD FILES ==================
|
# ================== CLEANUP OLD FILES ==================
|
||||||
for old_file in EXPORT_DIR.glob("*Dekurz (poslední rok)*.xlsx"):
|
for old_file in EXPORT_DIR.glob("*Dekurz (poslední rok)*.xlsx"):
|
||||||
try:
|
try:
|
||||||
@@ -107,11 +109,12 @@ for old_file in EXPORT_DIR.glob("*Dekurz (poslední rok)*.xlsx"):
|
|||||||
print(f"🧹 Deleted old file: {old_file.name}")
|
print(f"🧹 Deleted old file: {old_file.name}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"⚠️ Could not delete {old_file.name}: {e}")
|
print(f"⚠️ Could not delete {old_file.name}: {e}")
|
||||||
|
|
||||||
# ================== EXPORT TO EXCEL ==================
|
# ================== EXPORT TO EXCEL ==================
|
||||||
with pd.ExcelWriter(xlsx_path, engine="openpyxl") as writer:
|
with pd.ExcelWriter(xlsx_path, engine="openpyxl") as writer:
|
||||||
df.to_excel(writer, index=False, sheet_name="Dekurz")
|
df.to_excel(writer, index=False, sheet_name="Dekurz")
|
||||||
ws = writer.sheets["Dekurz"]
|
ws = writer.sheets["Dekurz"]
|
||||||
|
ws.freeze_panes = "F2" #zamčení prvního řádku a sloupců A:F
|
||||||
# ----- Bright yellow header -----
|
# ----- Bright yellow header -----
|
||||||
header_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
|
header_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
|
||||||
for cell in ws[1]:
|
for cell in ws[1]:
|
||||||
|
|||||||
@@ -7,14 +7,10 @@ Show decoded physician notes (RTF → plain text) directly in console.
|
|||||||
|
|
||||||
import fdb
|
import fdb
|
||||||
from striprtf.striprtf import rtf_to_text
|
from striprtf.striprtf import rtf_to_text
|
||||||
|
from Functions import get_medicus_connection
|
||||||
|
|
||||||
# ===== connection =====
|
# ===== connection =====
|
||||||
con = fdb.connect(
|
con = get_medicus_connection()
|
||||||
dsn='localhost:z:\\Medicus 3\\data\\medicus.fdb',
|
|
||||||
user='sysdba',
|
|
||||||
password='masterkey',
|
|
||||||
charset='WIN1250'
|
|
||||||
)
|
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
|
|
||||||
# ===== pick a few recent records =====
|
# ===== pick a few recent records =====
|
||||||
|
|||||||
Reference in New Issue
Block a user