TH1/Tools/ExportStringToExcel.py
2026-05-10 11:52:37 +08:00

71 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
import sys
import io
# 强制 UTF-8 输出
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
import openpyxl
import os
from datetime import datetime
def log(message):
print(f"[{datetime.now()}] {message}")
def parse_special_format(content):
"""解析特殊标记格式的数据。
新 TXT 格式 16 字段(编辑器 sb.Append 写出,去掉 RU/ES/PT/FR + 加 IsSecondary
0=ID 1=活跃 2=ZH 3=TDZH 4=EN 5=JP 6=KR 7=IsSecondary
8=IsProperNoun 9=IsDialogue 10=DialogueSpeaker 11=IsDeprecated
12=IsCustom 13=IsSpecialTerm 14=Color 15=Icon
编辑器写出时还会附 16=Desc备注所以读到的字段数可能是 16 或 17。"""
records = []
for record in content.split('!@#$%'):
if not record.strip():
continue
fields = [f.strip() for f in record.split('%$#@!')]
if len(fields) in (16, 17):
# 不足 17 时补上空备注列
if len(fields) == 16:
fields.append("")
records.append(fields)
else:
log(f"忽略格式错误的记录(字段数={len(fields)}): {record[:60]}")
return records
def convert_file():
try:
txt_path = r"../Tools/MultilingualTxt.txt"
excel_path = r"../Tools/Multilingual.xlsx"
with open(txt_path, 'r', encoding='utf-8') as f:
content = f.read()
records = parse_special_format(content)
if not records:
log("错误:未找到有效记录")
return False
wb = openpyxl.Workbook()
ws = wb.active
# 17 列:删 RU/ES/PT/FR/德语 4 列,新增"次要文案"列
ws.append([
"ID", "活跃文本", "中文", "繁中", "英文", "日文", "韩文",
"次要文案",
"专有翻译", "台词", "台词说话者", "无需翻译", "自定义条目", "专有名词",
"颜色", "图标", "备注",
])
for row in records:
ws.append(row)
os.makedirs(os.path.dirname(excel_path), exist_ok=True)
wb.save(excel_path)
log(f"成功转换 {len(records)} 条记录")
return True
except Exception as e:
print(f"转换失败: {str(e)}", file=sys.stderr) # 只有错误才输出到 stderr
return False
if __name__ == "__main__":
sys.exit(0 if convert_file() else 1)