114 lines
4.1 KiB
Python
114 lines
4.1 KiB
Python
#!/usr/bin/env python
|
|
"""Export the treatment-plan summary (orders_output + docs_output) as a
|
|
formatted .xlsx workbook, with the surgery date column front and center.
|
|
|
|
Usage:
|
|
python export_excel.py --orders-dir orders_output --docs-dir docs_output --out treatment_summary.xlsx
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from openpyxl import Workbook
|
|
from openpyxl.styles import Alignment, Font, PatternFill
|
|
from openpyxl.utils import get_column_letter
|
|
|
|
from analyze_treatment import PatientSummary, analyze_docs, analyze_orders
|
|
|
|
COLUMNS = [
|
|
("住院号", 14),
|
|
("姓名", 10),
|
|
("治疗方案推断", 26),
|
|
("是否手术", 9),
|
|
("手术日期", 12),
|
|
("是否化疗", 9),
|
|
("化疗药物", 18),
|
|
("是否放疗", 9),
|
|
("是否免疫治疗", 11),
|
|
("免疫药物", 18),
|
|
("是否靶向治疗", 11),
|
|
("靶向药物", 16),
|
|
("出院诊断", 24),
|
|
("TNM分期", 10),
|
|
("手术名称(含操作细节,截断)", 60),
|
|
]
|
|
|
|
|
|
def row_values(s: PatientSummary) -> list:
|
|
return [
|
|
s.patient_id, s.name, s.plan_label(),
|
|
"是" if s.has_surgery else "否", s.surgery_date,
|
|
"是" if s.has_chemo else "否", "、".join(sorted(s.chemo_drugs)),
|
|
"是" if s.has_radio else "否",
|
|
"是" if s.has_immuno else "否", "、".join(sorted(s.immuno_drugs)),
|
|
"是" if s.has_targeted else "否", "、".join(sorted(s.targeted_drugs)),
|
|
s.discharge_dx, s.tnm,
|
|
s.surgery_name,
|
|
]
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--orders-dir", type=Path, default=Path("orders_output"))
|
|
parser.add_argument("--docs-dir", type=Path, default=Path("docs_output"))
|
|
parser.add_argument("--out", type=Path, default=Path("treatment_summary.xlsx"))
|
|
args = parser.parse_args()
|
|
|
|
order_files = {fp.stem: fp for fp in args.orders_dir.glob("*.json")}
|
|
doc_files = {fp.stem: fp for fp in args.docs_dir.glob("*.json")}
|
|
patient_ids = sorted(set(order_files) | set(doc_files))
|
|
|
|
summaries = []
|
|
for pid in patient_ids:
|
|
summary = PatientSummary(patient_id=pid, name="")
|
|
if pid in order_files:
|
|
data = json.loads(order_files[pid].read_text(encoding="utf-8"))
|
|
summary.name = data.get("name", summary.name)
|
|
analyze_orders(data, summary)
|
|
if pid in doc_files:
|
|
data = json.loads(doc_files[pid].read_text(encoding="utf-8"))
|
|
summary.name = summary.name or data.get("name", "")
|
|
analyze_docs(data, summary)
|
|
summaries.append(summary)
|
|
|
|
# Surgery patients first (sorted by surgery date), then everyone else by patient_id
|
|
summaries.sort(key=lambda s: (0, s.surgery_date, s.patient_id) if s.has_surgery else (1, "", s.patient_id))
|
|
|
|
wb = Workbook()
|
|
ws = wb.active
|
|
ws.title = "治疗方案汇总"
|
|
|
|
header_font = Font(bold=True, color="FFFFFF")
|
|
header_fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
|
|
surgery_fill = PatternFill(start_color="E2EFDA", end_color="E2EFDA", fill_type="solid")
|
|
|
|
for col_idx, (title, width) in enumerate(COLUMNS, start=1):
|
|
cell = ws.cell(row=1, column=col_idx, value=title)
|
|
cell.font = header_font
|
|
cell.fill = header_fill
|
|
cell.alignment = Alignment(horizontal="center", vertical="center")
|
|
ws.column_dimensions[get_column_letter(col_idx)].width = width
|
|
|
|
for row_idx, s in enumerate(summaries, start=2):
|
|
values = row_values(s)
|
|
for col_idx, value in enumerate(values, start=1):
|
|
cell = ws.cell(row=row_idx, column=col_idx, value=value)
|
|
cell.alignment = Alignment(vertical="top", wrap_text=(col_idx == len(COLUMNS)))
|
|
if s.has_surgery and col_idx == 5: # 手术日期 column
|
|
cell.fill = surgery_fill
|
|
cell.font = Font(bold=True)
|
|
|
|
ws.freeze_panes = "A2"
|
|
ws.auto_filter.ref = ws.dimensions
|
|
|
|
wb.save(args.out)
|
|
print(f"{len(summaries)} patients -> {args.out}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|