This commit is contained in:
2026-04-10 17:06:01 +02:00
parent bafd58d2b7
commit c4912744ea
29 changed files with 62 additions and 22 deletions
+19 -7
View File
@@ -5,10 +5,13 @@ from openpyxl import load_workbook
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.utils import get_column_letter
INVENTORY_DIR = Path("xls_reports")
DESTRUCTION_DIR = Path("xls_ip_destruction")
# STUDY = "42847922MDD3003"
STUDY = "77242113UCO3001"
INVENTORY_DIR = Path(f"xls_reports_{STUDY}")
DESTRUCTION_DIR = Path(f"xls_ip_destruction_{STUDY}")
OUTPUT_DIR = Path("output")
OUTPUT_FILE = OUTPUT_DIR / f"{date.today().strftime('%Y-%m-%d')} 42847922MDD3003 CZ IWRS overview.xlsx"
OUTPUT_FILE = OUTPUT_DIR / f"{date.today().strftime('%Y-%m-%d')} {STUDY} CZ IWRS overview.xlsx"
# ── Shared constants ──────────────────────────────────────────────────────────
@@ -27,6 +30,7 @@ COLUMN_RENAMES = {
"Assignment User": "Asgn User",
"Dispensation Status": "Disp Status",
"Dispensing Date": "Disp Date",
"Dispensing date": "Disp Date",
"Quantity Dispensed": "Qty Disp",
"Dispensing User": "Disp User",
"Quantity Returned": "Qty Ret",
@@ -70,13 +74,19 @@ COLUMN_WIDTHS = {
def read_inventory(path):
df = pd.read_excel(path, header=None)
header_row = df[df[0] == "Medication ID"].index[0]
data = pd.read_excel(path, header=header_row)
# Support both "Medication ID" (MDD3003) and "Medication" (UCO3001)
mask = df[0].isin(["Medication ID", "Medication"])
meta = {}
for i in range(header_row):
for i in range(len(df)):
val = str(df.iloc[i, 0]) if pd.notna(df.iloc[i, 0]) else ""
if val.startswith("Site:"):
meta["site"] = val.replace("Site:", "").strip()
if not mask.any():
print(f" {path.name}: no data (skipping)")
return None, meta
header_row = df[mask].index[0]
data = pd.read_excel(path, header=header_row)
data = data.rename(columns={"Medication": "Medication ID"})
return data, meta
@@ -140,6 +150,8 @@ def build_main(lookup):
all_rows = []
for path in sorted(INVENTORY_DIR.glob("onsite_inventory_detail_*.xlsx")):
df, meta = read_inventory(path)
if df is None:
continue
df["DestroyedOn"] = df["Medication ID"].apply(
lambda x: lookup.get(int(x), (None, None))[1] if pd.notna(x) else None)
df["Basket number"] = df["Medication ID"].apply(
@@ -208,7 +220,7 @@ def build_kits_for_destruction(df):
def main():
# Prepare output dir, remove any previous overview file
OUTPUT_DIR.mkdir(exist_ok=True)
for old in OUTPUT_DIR.glob("*42847922MDD3003 CZ IWRS overview.xlsx"):
for old in OUTPUT_DIR.glob(f"*{STUDY} CZ IWRS overview.xlsx"):
old.unlink()
print(f"Removed old file: {old.name}")