TH1/Tools/ExportStringToExcel.py
2026-04-08 20:15:08 +08:00

59 lines
2.0 KiB
Python

# -*- 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):
"""解析特殊标记格式的数据"""
records = []
# 按记录分割(注意保留结尾空行处理)
for record in content.split('!@#$%'):
if not record.strip():
continue
# 按字段分割
fields = [f.strip() for f in record.split('%$#@!')]
if len(fields) == 20: # ID,ZH,EN,JP,KR
records.append(fields)
else:
log(f"忽略格式错误的记录: {record}")
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
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)