vampire-like/数据表/convert.py

44 lines
1.5 KiB
Python

import pandas as pd
import os
def convert_excel_to_txt(folder_path='.'):
# 计数器,用于最后汇总
count = 0
target_dir = os.path.join(os.path.dirname(__file__), '../Assets/GameMain/DataTables')
target_dir = os.path.abspath(target_dir)
# 确保目标目录存在
os.makedirs(target_dir, exist_ok=True)
for dirpath, _, filenames in os.walk(folder_path):
for file_name in filenames:
if not file_name.endswith(('.xlsx', '.xls')):
continue
file_path = os.path.join(dirpath, file_name)
txt_name = os.path.splitext(file_name)[0] + '.txt'
target_file = os.path.join(target_dir, txt_name)
print(f"正在处理: {file_name}...")
try:
df = pd.read_excel(file_path, header=None, keep_default_na=False)
with open(target_file, 'w', encoding='utf-8', newline='') as f:
for row in df.itertuples(index=False):
line = '\t'.join('' if cell is None else str(cell) for cell in row)
f.write(line + '\n')
print(f"成功转换 -> {target_file}")
count += 1
except Exception as e:
print(f"处理 {file_name} 时出错: {e}")
print(f"\n任务完成!共转换了 {count} 个文件。")
if __name__ == "__main__":
convert_excel_to_txt('.')
# --- 关键修改:在这里添加暂停 ---
print("\n" + "="*30)
input("按回车键(Enter)退出程序...")