c1_unity/Tools/localization/exceltolua.py
2023-04-03 11:04:31 +08:00

93 lines
3.7 KiB
Python

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import sys
current_file_path = os.path.split(os.path.realpath(__file__))[0]
site_packages_path = os.path.abspath(os.path.join(current_file_path, "../site-packages"))
sys.path.append(site_packages_path)
import csv
import re
from openpyxl import load_workbook
reload(sys)
sys.setdefaultencoding('utf8')
def do(excelFolderPath, luaFolderPath, isDev):
if not os.path.exists(excelFolderPath):
print("can't find excelPath!")
return
writeKey = False
for root, dirs, files in os.walk(excelFolderPath, True):
for name in files:
if name[0:11] == "global.xlsx":
file_path = os.path.join(root, name)
excel_data = load_workbook(file_path, data_only=True)
sheet = excel_data[excel_data.sheetnames[0]]
max_row = sheet.max_row
max_column = sheet.max_column
file_name = os.path.basename(file_path).split('.')[0]
lua_name = "%s.%s" % (file_name, "lua")
lua_name = lua_name.lower()
lua_path = root.replace(excelFolderPath, luaFolderPath)
none_count = 0
language_list = []
for column in range(2, max_column + 1):
if sheet.cell(2, column).value == "none":
continue
language_str = sheet.cell(3, column).value
split_str = language_str.split('-')
if len(split_str) != 2:
raise Exception('global name is not xx-xx(language)')
if language_str == None:
none_count = none_count + 1
if none_count > 10:
raise Exception('global column more than ten column')
lua_path1 = os.path.join(lua_path, split_str[1])
if isDev == "0":
lua_path1 = os.path.join(lua_path1, lua_name + ".bytes")
else:
lua_path1 = os.path.join(lua_path1, lua_name)
lua = open(lua_path1, 'w')
lua_table_name = "local localization_global"
lua.write("%s = \n{\n" %(lua_table_name))
for i in range(5, max_row + 1):
cell_value = sheet.cell(i, column).value
if cell_value != None:
lua.write(" [\"%s\"] = \"%s\",\n" %(sheet.cell(i, 1).value, cell_value))
lua.write("}\n\n")
lua.write("return localization_global")
lua.close()
if not writeKey:
constPath = luaFolderPath + "/localization/localization_global_const.lua"
if isDev == "0":
constPath += ".bytes"
lua1 = open(constPath, 'w')
lua_table_name = "local LocalizationGlobalConst"
lua1.write("%s = \n{\n" %(lua_table_name))
for i in range(5, max_row + 1):
lua1.write(" %s = \"%s\",\n" %(sheet.cell(i, 1).value.upper(), sheet.cell(i, 1).value))
lua1.write("}\n\n")
lua1.write("return LocalizationGlobalConst")
lua1.close()
writeKey = True
excel_data.close()
if __name__ == '__main__':
args = sys.argv
if len(args) < 4:
sys.exit(1)
do(args[1], args[2], args[3])