35 lines
957 B
Python
35 lines
957 B
Python
"""从 Excel 提取医院-科室-专家原始数据到 hospitals-excel-raw.json。"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from openpyxl import load_workbook
|
|
|
|
WEB = Path(__file__).resolve().parents[1]
|
|
DOCS = WEB / "public" / "documents"
|
|
OUT = Path(__file__).resolve().parent / "hospitals-excel-raw.json"
|
|
|
|
|
|
def find_xlsx() -> Path:
|
|
files = sorted(DOCS.glob("*.xlsx"))
|
|
if not files:
|
|
raise SystemExit(f"找不到 Excel: {DOCS}")
|
|
return files[-1]
|
|
|
|
|
|
def main() -> None:
|
|
xlsx = find_xlsx()
|
|
wb = load_workbook(xlsx, read_only=True, data_only=True)
|
|
ws = wb.active
|
|
rows: list[list] = []
|
|
for row in ws.iter_rows(values_only=True):
|
|
rows.append([cell if cell is not None else None for cell in row])
|
|
|
|
OUT.write_text(json.dumps(rows, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
print(f"OK: {xlsx.name} -> {len(rows)} rows -> {OUT}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|