102 lines
2.9 KiB
Python
102 lines
2.9 KiB
Python
# pyright: reportMissingImports=false
|
|||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from pathlib import Path
|
||
|
|
import re
|
||
|
|
|
||
|
|
from openpyxl import load_workbook
|
||
|
|
|
||
|
|
|
||
|
|
ROOT = Path(__file__).resolve().parent
|
||
|
|
SOURCE_FILE = ROOT.parent / "202606 Conti Retail APP Component data source.xlsx"
|
||
|
|
OUTPUT_FILE = ROOT / f"{SOURCE_FILE.stem}.md"
|
||
|
|
|
||
|
|
FIELDS = [
|
||
|
|
("编号", 2),
|
||
|
|
("模块", 3),
|
||
|
|
("功能", 4),
|
||
|
|
("负责人", 5),
|
||
|
|
("前置任务", 6),
|
||
|
|
("数据集", 7),
|
||
|
|
("来源", 8),
|
||
|
|
("安全", 9),
|
||
|
|
("备注", 10),
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
def normalize_cell(value: object) -> str:
|
||
|
|
if value is None:
|
||
|
|
return ""
|
||
|
|
|
||
|
|
if isinstance(value, float) and value.is_integer():
|
||
|
|
value = int(value)
|
||
|
|
|
||
|
|
text = str(value).replace("\r\n", "\n").replace("\r", "\n")
|
||
|
|
lines = [re.sub(r"\s+", " ", line).strip() for line in text.split("\n")]
|
||
|
|
return "\n".join(line for line in lines if line)
|
||
|
|
|
||
|
|
|
||
|
|
def add_field(parts: list[str], name: str, value: str) -> None:
|
||
|
|
if not value:
|
||
|
|
return
|
||
|
|
|
||
|
|
parts.append(f"**{name}**")
|
||
|
|
parts.append("")
|
||
|
|
|
||
|
|
lines = [line.strip() for line in value.splitlines() if line.strip()]
|
||
|
|
if len(lines) > 1:
|
||
|
|
parts.extend(f"- {line}" for line in lines)
|
||
|
|
else:
|
||
|
|
parts.extend(lines)
|
||
|
|
|
||
|
|
parts.append("")
|
||
|
|
|
||
|
|
|
||
|
|
def extract_workbook_to_md() -> None:
|
||
|
|
workbook = load_workbook(SOURCE_FILE, data_only=True)
|
||
|
|
parts: list[str] = [f"# {SOURCE_FILE.stem}", ""]
|
||
|
|
|
||
|
|
for worksheet in workbook.worksheets:
|
||
|
|
rows: list[dict[str, str]] = []
|
||
|
|
|
||
|
|
for row_index in range(2, worksheet.max_row + 1):
|
||
|
|
row = {
|
||
|
|
field: normalize_cell(worksheet.cell(row=row_index, column=column).value)
|
||
|
|
for field, column in FIELDS
|
||
|
|
}
|
||
|
|
if not any(row.values()):
|
||
|
|
continue
|
||
|
|
if not any(row[key] for key in ("编号", "模块", "功能")):
|
||
|
|
continue
|
||
|
|
rows.append(row)
|
||
|
|
|
||
|
|
if not rows:
|
||
|
|
continue
|
||
|
|
|
||
|
|
parts.append(f"## {worksheet.title}")
|
||
|
|
parts.append("")
|
||
|
|
|
||
|
|
for row in rows:
|
||
|
|
number = row["编号"]
|
||
|
|
module = row["模块"]
|
||
|
|
title = " ".join(part for part in (number, module) if part).strip()
|
||
|
|
level = min(6, 3 + number.count(".")) if number else 3
|
||
|
|
|
||
|
|
parts.append(f"{'#' * level} {title or 'Untitled'}")
|
||
|
|
parts.append("")
|
||
|
|
add_field(parts, "功能", row["功能"])
|
||
|
|
add_field(parts, "负责人", row["负责人"])
|
||
|
|
add_field(parts, "前置任务", row["前置任务"])
|
||
|
|
add_field(parts, "数据集", row["数据集"])
|
||
|
|
add_field(parts, "来源", row["来源"])
|
||
|
|
add_field(parts, "安全", row["安全"])
|
||
|
|
add_field(parts, "备注", row["备注"])
|
||
|
|
|
||
|
|
OUTPUT_FILE.write_text("\n".join(parts).strip() + "\n", encoding="utf-8")
|
||
|
|
print("Wrote workbook markdown output")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
extract_workbook_to_md()
|