# 导入所需模块
import xlwt
import os
import lupa
# 创建LuaRuntime实例
lua = lupa.LuaRuntime(unpack_returned_tuples=True)
# 定义文件名和表头映射关系
fileNameMap = {
"cfg_model_info": [
"index", "id", "名称", "type", "类型", "sex", "dir", "hud_top", "blendmode", "stand_pos_x", "stand_pos_y",
"idle_interval", "walk_interval", "run_interval", "attack_interval", "magic_interval", "die_interval",
"stuck_interval", "sitdown_interval", "mining_interval", "born_interval", "showstand_interval",
"ready_interval", "changeshape_interval", "enlarge", "offsetx", "offsety", "closefx", "shadow"
],
}
# 获取脚本文件所在的目录
script_directory = os.path.dirname(os.path.abspath(__file__))
# 改变当前工作目录
os.chdir(script_directory)
print("按下回车键开始...")
# 暂停程序执行,等待用户按下回车键继续
input()
# 遍历当前目录下的.lua文件
for file in os.listdir(os.getcwd()):
if file.endswith('.lua'):
fileName = file.split('.')[0]
if fileName not in fileNameMap:
print(file, "is not in fileNameMap")
continue
with open(file, 'r') as f:
lua_code = f.read()
# 执行lua代码
try:
lua_table = lua.execute(lua_code)
except lupa._lupa.LuaError:
print(file, "Lua code execution error")
continue
# 不为空
if not lua_table:
print(file, "is empty")
continue
print("-------------开始转换-------------")
print("-------------", file, "-------------")
# 创建一个工作簿对象
workbook = xlwt.Workbook()
# 创建一个工作表对象
worksheet = workbook.add_sheet(fileName)
# 写入表头
keys = fileNameMap[fileName]
print("表头顺序\n", keys)
for i, key in enumerate(keys):
worksheet.write(0, i, key)
# 写入数据,即值的内容
for i, value in enumerate(lua_table.values()):
if not isinstance(value, dict):
print(file, "contains invalid data")
break
for k, v in value.items():
# 根据key获取索引
if k not in keys:
continue
index = keys.index(k)
# 写入数据到对应位置
worksheet.write(i + 1, index, str(v))
# 保存xls文件
workbook.save(fileName + '.xls')
print("已完成转换:", fileName + '.xls')
print("完成!!!")
# 暂停程序执行,等待用户按下回车键继续
input()