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

53 lines
1.9 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}") # 移除 file=sys.stderr
def convert_excel_to_txt():
try:
excel_path = r"../Tools/Multilingual.xlsx"
txt_path = r"../Tools/MultilingualTxt.txt"
wb = openpyxl.load_workbook(excel_path)
ws = wb.active
# xlsx 17 列ID, 活跃, 中文, 繁中, 英文, 日文, 韩文, 次要文案,
# 专翻, 台词, 说话者, 无需翻, 自定义, 专名, 颜色, 图标, 备注
# TXT 16 字段:丢掉最末备注列(编辑器侧 ExcelExportToAsset 不需要备注)
TXT_FIELD_COUNT = 16
records = []
for row in ws.iter_rows(min_row=2, values_only=True):
normalized_row = [str(cell).strip() if cell is not None else "" for cell in row]
if len(normalized_row) >= TXT_FIELD_COUNT:
records.append(normalized_row[:TXT_FIELD_COUNT])
else:
records.append(normalized_row + [""] * (TXT_FIELD_COUNT - len(normalized_row)))
if not records:
log("错误:Excel中没有有效数据")
return False
os.makedirs(os.path.dirname(txt_path), exist_ok=True)
with open(txt_path, 'w', encoding='utf-8') as f:
for record in records:
line = '%$#@!'.join(field for field in record) + '!@#$%'
f.write(line)
log(f"成功转换 {len(records)} 条记录到TXT")
return True
except Exception as e:
print(f"转换失败: {str(e)}", file=sys.stderr) # 只有错误才输出到 stderr
return False
if __name__ == "__main__":
sys.exit(0 if convert_excel_to_txt() else 1)