# -*- coding: utf-8 -*-
"""
德国大众 CRA 问卷导出脚本
作用：把 xlsx 全部内容（所有 sheet / 单元格 / 合并区域）导出为纯文本，
      存到 d:\开发\OnebotCatalog\cra_dump.txt，方便 AI 直接读取并翻译。
"""
import sys, io, os

# 强制 UTF-8 输出，避免中文/德语乱码
try:
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
except Exception:
    pass

SRC = r"C:\Users\ruigu\Downloads\文档说明.html\20260826德国大众的要求文件\MAGSWITCH TECHNOLOGY EUROPE_CRA_Abfrage.xlsx"
OUT = r"d:\开发\OnebotCatalog\cra_dump.txt"

def main():
    if not os.path.exists(SRC):
        print("!! 找不到源文件：", SRC)
        return

    try:
        import openpyxl
    except ImportError:
        print("!! 缺少 openpyxl，正在尝试安装...")
        import subprocess
        subprocess.check_call([sys.executable, "-m", "pip", "install", "openpyxl", "-q"])
        import openpyxl

    wb = openpyxl.load_workbook(SRC, data_only=False)

    lines = []
    lines.append("FILE: " + SRC)
    lines.append("SHEETS: " + " | ".join(wb.sheetnames))
    lines.append("=" * 80)

    for ws in wb.worksheets:
        lines.append("")
        lines.append("#" * 80)
        lines.append(f"## SHEET: {ws.title}   (max_row={ws.max_row}, max_col={ws.max_column})")
        lines.append("#" * 80)

        # 合并单元格信息
        if ws.merged_cells.ranges:
            lines.append("MERGED: " + ", ".join(str(r) for r in ws.merged_cells.ranges))

        for row in ws.iter_rows():
            cells = []
            for c in row:
                v = c.value
                if v is None:
                    v = ""
                v = str(v).replace("\n", "\\n")
                if v != "":
                    cells.append(f"{c.coordinate}={v}")
            if cells:
                lines.append(" | ".join(cells))

    text = "\n".join(lines)
    with open(OUT, "w", encoding="utf-8") as f:
        f.write(text)

    print("DONE")
    print("已导出到:", OUT)
    print("总行数:", len(lines))
    print("---- 内容预览（前 60 行）----")
    for l in lines[:60]:
        print(l)

if __name__ == "__main__":
    main()
