-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxls2lua.py
111 lines (91 loc) · 2.96 KB
/
xls2lua.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# Author: NormanYang
import xlrd
import os.path
import time
import os
import sys
import codecs
LANGUAGE_NAME = "LangManager"
SCRIPT_HEAD = "-- This file is generated by program!\n\
-- Don't change it manaully.\n\
-- Author: zerospace007@163.com NormanYang\n\
-- Source file: %s\n\
-- Created at: %s\n\
\n\
\n\
"
def make_table(filename):
if not os.path.isfile(filename):
sys.exit("%s is not a valid filename" % filename)
book_xlrd = xlrd.open_workbook(filename)
excel = {}
excel["filename"] = filename
excel["data"] = {}
sheets = book_xlrd.sheets()
sheet_nums = book_xlrd.nsheets
sheet_idx = 0;
for sheet_idx in range(sheet_nums):
sheet = sheets[sheet_idx]
sheet_name = sheet.name.replace(" ", "_")
if not sheet_name.startswith("OUT_"):
continue
sheet_name = sheet_name[4:]
print(sheet_name+" sheet")
excel["data"][sheet_idx] = {}
# 必须至少2列,一列key键值,一列value值
if sheet.ncols < 2:
return {}, -1, "sheet[" + sheet_name + "]" + " columns must >= 2"
row_idx = 0
# 数据从第1行开始
for row_idx in range(0, sheet.nrows):
row = {}
col_idx = 0
for col_idx in range(sheet.ncols):
value = sheet.cell_value(row_idx, col_idx)
# 本行有数据
row[col_idx] = format_str(value)
excel["data"][sheet_idx][row_idx] = row
return excel, 0 , "ok"
def format_str(value):
if type(value) == int or type(value) == float:
value = str(value)
value = value.replace('\"','\\\"')
value = value.replace('\'','\\\'')
return value
def get_string(value):
if value is None:
return ""
return value
def write_to_lua_script(excel, output_path):
if not os.path.exists(output_path):
os.mkdir(output_path)
output_filename = output_path + "/" + LANGUAGE_NAME + ".lua"
outfp = codecs.open(output_filename, 'w', 'UTF-8')
#写入文件头
# create_time = time.strftime("%a %b %d %H:%M:%S %Y", time.gmtime(time.time()))
create_time = time.strftime("%b %Y", time.gmtime(time.time()))
outfp.write(SCRIPT_HEAD % (excel["filename"], create_time))
outfp.write("LangManager = {}\nlocal this = LangManager\n")
outfp.write("\n")
for (sheet_name, sheet) in excel["data"].items():
for (row_idx, row) in sheet.items():
outfp.write("this[" + "\"" + str(get_string(row[0])) + "\"" + "] = ")
outfp.write("\"" + str(get_string(row[1])) + "\"\n")
outfp.write("\nfunction LangManager.GetText(key, ...)\n\tif(this[key] ~= nil and string.len(this[key]) > 0) then\n\t\treturn string.format(this[key], ...);\n\tend\n\treturn key;\nend\n")
outfp.write("\nfunction LangManager.GetTempText(key, ...)\n\treturn string.format(key, ...);\nend\n")
outfp.close()
def handler_file(excel_name, output_path):
data, ret, errstr = make_table(excel_name)
if ret != 0:
print(excel_name)
print("error: " + errstr)
else:
print(excel_name)
print("res:")
# print(data)
print("success!!!")
write_to_lua_script(data, output_path)
if __name__=="__main__":
handler_file(LANGUAGE_NAME + ".xlsx", "./")