TH1/Tools/ExportStringToExcel.py

53 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
import sys
import openpyxl
import os
from datetime import datetime
def log(message):
print(f"[{datetime.now()}] {message}", file=sys.stderr)
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) == 6: # 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:
log(f"转换失败: {str(e)}")
return False
if __name__ == "__main__":
sys.exit(0 if convert_file() else 1)