98 lines
2.9 KiB
Python
98 lines
2.9 KiB
Python
import pandas as pd
|
|
from datetime import date
|
|
from openpyxl import load_workbook
|
|
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
|
|
from openpyxl.utils import get_column_letter
|
|
|
|
SOURCE_FILE = "accountability_combined.xlsx"
|
|
OUTPUT_FILE = "sheet_expired.xlsx"
|
|
|
|
DATE_COLUMNS = {
|
|
"Orig Exp Date", "Exp Date", "Rcv Date",
|
|
"Date Asgn", "Disp Date", "Date Ret", "Destroyed",
|
|
}
|
|
|
|
COLUMN_WIDTHS = {
|
|
"Site": 14,
|
|
"Med ID": 10,
|
|
"Lot No.": 12,
|
|
"Orig Exp Date": 16,
|
|
"Exp Date": 14,
|
|
"Rcv Date": 14,
|
|
"Rcpt User": 22,
|
|
"Subject ID": 14,
|
|
"Qty Asgn": 9,
|
|
"IRT Tx": 8,
|
|
"Date Asgn": 14,
|
|
"Asgn User": 20,
|
|
"Disp Status": 16,
|
|
"Disp Date": 14,
|
|
"Qty Disp": 9,
|
|
"Disp User": 20,
|
|
"Qty Ret": 10,
|
|
"Date Ret": 14,
|
|
"Ret User": 18,
|
|
"Destroyed": 14,
|
|
"Basket No.": 12,
|
|
}
|
|
|
|
today = date.today()
|
|
sheet_name = f"Expired as of {today.strftime('%d-%b-%Y')}"
|
|
|
|
# Load source
|
|
df = pd.read_excel(SOURCE_FILE)
|
|
|
|
# Convert date columns
|
|
for col in DATE_COLUMNS:
|
|
if col in df.columns:
|
|
df[col] = pd.to_datetime(df[col], errors="coerce")
|
|
|
|
# Filter: not in basket AND not assigned to patient AND Exp Date < today
|
|
mask = df["Basket No."].isna() & df["Subject ID"].isna() & (df["Exp Date"] < pd.Timestamp(today))
|
|
filtered = df[mask].copy().reset_index(drop=True)
|
|
|
|
print(f"Expired kits not in basket: {len(filtered)}")
|
|
|
|
filtered.to_excel(OUTPUT_FILE, index=False, sheet_name=sheet_name)
|
|
|
|
# Formatting
|
|
wb = load_workbook(OUTPUT_FILE)
|
|
ws = wb[sheet_name]
|
|
|
|
header_fill = PatternFill("solid", start_color="C00000") # dark red
|
|
header_font = Font(bold=True, color="FFFFFF", name="Arial", size=10)
|
|
row_font = Font(name="Arial", size=10)
|
|
exp_fill = PatternFill("solid", start_color="FFE0E0") # light red highlight for Exp Date
|
|
|
|
thin = Side(style="thin", color="000000")
|
|
border = Border(left=thin, right=thin, top=thin, bottom=thin)
|
|
|
|
headers = [cell.value for cell in ws[1]]
|
|
|
|
for cell in ws[1]:
|
|
cell.fill = header_fill
|
|
cell.font = header_font
|
|
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=False)
|
|
cell.border = border
|
|
|
|
for row in ws.iter_rows(min_row=2, max_row=ws.max_row):
|
|
for cell in row:
|
|
col_name = headers[cell.column - 1] if cell.column <= len(headers) else None
|
|
cell.font = row_font
|
|
cell.border = border
|
|
cell.alignment = Alignment(horizontal="center")
|
|
if col_name in DATE_COLUMNS:
|
|
cell.number_format = "DD-MMM-YYYY"
|
|
if col_name == "Exp Date":
|
|
cell.fill = exp_fill
|
|
|
|
for cell in ws[1]:
|
|
width = COLUMN_WIDTHS.get(cell.value, 14)
|
|
ws.column_dimensions[get_column_letter(cell.column)].width = width
|
|
|
|
ws.auto_filter.ref = ws.dimensions
|
|
ws.freeze_panes = "A2"
|
|
|
|
wb.save(OUTPUT_FILE)
|
|
print(f"Saved: {OUTPUT_FILE} (sheet: '{sheet_name}')")
|