-
Notifications
You must be signed in to change notification settings - Fork 0
/
XlsToLua.py
358 lines (258 loc) · 10.2 KB
/
XlsToLua.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/python3
import xlrd
import sys
import os
import re
import datetime
import time
import getopt
import argparse
today = datetime.date.today
now = time.strftime("%H:%M:%S")
dirName = time.strftime("%Y%m%d-%H%M%S", time.localtime())
header = False
print('dn: ' + dirName)
fileError = False
dataStartRow = 4
dataNameRow = 1
dataTypeRow = 2
indent = 0
def line(strs):
return strs + '\n'
writeData = line('---@')
annotaionData = line('')
luaRequirefileName = 'LuaDataRequire.lua.txt'
luaRequireData = line('--- require')
def AddIdx(i):
global writeData
ShowIndent()
writeData += '[{idx}]'.format(idx = i) + ' = '
def AddKey(key):
global writeData
writeData += '{key} = '.format(key = key)
def AddValue(value, t = 0):
global writeData
if t == 0:
writeData += '{v},\n'.format(v = value)
elif t == 2:
if value == 'TRUE' or 1 or 'true' or 'True':
value = 'true'
elif value == 'FALSE' or 0 or 'false' or 'Flase':
value = 'false'
writeData += '{v},\n'.format(v = value)
else:
writeData += "'{v}',\n".format(v = value)
def ShowIndent():
global indent
global writeData
for _ in range(indent):
writeData += ' '
def AddIndent(n = 0):
global indent
indent = indent + n
def SheetParseIdx(booksheet, row):
global writeData
global indent
#print('idx parse:{row}'.format(row = row))
dataType = str(booksheet.cell(dataTypeRow, 0).value)
ShowIndent()
if dataType == 'string':
writeData += '{idx} = '.format(idx = str(booksheet.cell(row, 0).value))
writeData += '{\n'
return True
elif dataType == 'int':
writeData += '[{idx}] = '.format(idx = int(booksheet.cell(row, 0).value))
writeData += '{\n'
return True
else:
print('ERROR: 第一列的类型非法,必须是int或者string类型..\n')
return False
def SheetParseCol(booksheet, row, col):
global writeData
global annotaionData
try:
key = str(booksheet.cell(dataNameRow, col).value)
if '=HL' in key:
return True
d = booksheet.cell(row, col).value
if d == None or d == '':
print(line(''))
print('找到一个空的表格!!: {row},{col}'.format(row = row, col = col))
print(line(''))
return True
valueType = str(booksheet.cell(dataTypeRow, col).value)
ShowIndent()
AddKey(key)
addcontent = False
if row == 4:
addcontent = True
if valueType == 'int':
print('找到一个int: {row},{col}'.format(row = row, col = col))
value = int(booksheet.cell(row, col).value)
AddValue(value)
if addcontent:
annotaionData += line('---@field ' + key + ' number')
elif valueType == 'string':
print('找到一个string: {row},{col}'.format(row = row, col = col))
value = str(booksheet.cell(row, col).value)
AddValue(value, 1)
if addcontent:
annotaionData += line('---@field ' + key + ' string')
elif valueType == 'float':
print('找到一个float: {row},{col}'.format(row = row, col = col))
value = float(booksheet.cell(row, col).value)
AddValue(value)
if addcontent:
annotaionData += line('---@field ' + key + ' number')
elif valueType == 'bool':
print('找到一个bool: {row},{col}'.format(row = row, col = col))
value = booksheet.cell(row, col).value
AddValue(value, 2)
if addcontent:
annotaionData += line('---@field ' + key + ' boolean')
else:
m = re.match('array', valueType)
if m != None:
print('找到一个Array: {row},{col}..'.format(row = row, col = col))
obj = re.search(r'\[.+\]', valueType)
s = obj.group()
arrayType = s[1:len(s)-1]
value = booksheet.cell(row, col).value.split(',')
t = 0
if arrayType == 'string':
t = 1
if addcontent:
annotaionData += line('---@field ' + key + ' string[]')
elif arrayType == 'bool':
t = 2
if addcontent:
annotaionData += line('---@field ' + key + ' boolean[]')
else:
if addcontent:
annotaionData += line('---@field ' + key + ' number[]')
writeData += '{\n'
AddIndent(1)
for idx in range(len(value)):
AddIdx(idx + 1)
AddValue(value[idx], t)
AddIndent(-1)
ShowIndent()
writeData += '},\n'
m = re.match('table', valueType)
if m != None:
print('找到一个Table: {row},{col}..'.format(row = row, col = col))
writeData += '{\n'
AddIndent(1)
ShowIndent()
value = booksheet.cell(row, col).value
AddValue(value)
AddIndent(-1)
ShowIndent()
writeData += '},\n'
if addcontent:
annotaionData += line('---@field ' + key + ' table<string, number>')
except ValueError:
print("############# O_o||| ==> 策划兄弟类型出错啦: {row},{col}..".format(row = row, col = col))
global fileError
fileError = True
return False
def SheetParse(fileName, booksheet):
global writeData
global annotaionData
global fileError
global luaRequireData
global cursheet
global newfoloder
global header
fileError = False
if header == False:
for col in range(booksheet.ncols):
writeData += '--- '
writeData += str(booksheet.cell(0,col).value) + '\t\t'
writeData += str(booksheet.cell(1,col).value) + '\t\t'
writeData += str(booksheet.cell(2,col).value) + '\n'
writeData += line('')
if header == False:
writeData += line(fileName + "Data = {}")
writeData += line(fileName + "Data." + cursheet + " = {")
annotaionData += line('---@class ' + fileName + "Data")
annotaionData += line('---@field ' + cursheet + "DataIns" )
annotaionData += line('')
annotaionData += line('---@class ' + cursheet + "DataIns" )
AddIndent(1)
for row in range(dataStartRow, booksheet.nrows):
SheetParseIdx(booksheet, row)
AddIndent(1)
for col in range(1, booksheet.ncols):
SheetParseCol(booksheet, row, col)
AddIndent(-1)
ShowIndent()
writeData += '},\n'
writeData += '}\n'
writeData += annotaionData
annotaionData = ''
AddIndent(-1)
header = True
return not fileError
#设置参数
parser = argparse.ArgumentParser()
parser .add_argument("-sh", "--sheet", dest = 'sheet', required = True, nargs='*', help= "parse sheet name")
parser .add_argument("-fd", "--folder", dest = 'folder', help= "create new folder", action = 'store_true')
args = parser .parse_args()
global targetSheetName
global newfoloder
global cursheet
dirName
print('args.sheet = {sheet}'.format(sheet = args.sheet))
targetSheetName = args.sheet
print('args.folder = {folder}'.format(folder = args.folder))
newfoloder = args.folder
ok = False
if newfoloder == True:
os.makedirs('./' + dirName)
for parent,dirnames,filenames in os.walk("."):
#print('parent = {parent}, dirnames = {dirnames}, filenames = {filenames}'.format(parent = parent, dirnames = dirnames, filenames = filenames))
for filename in filenames:
portion = os.path.splitext(filename)
ext = portion[1]
if ext == '.xlsx':
finddatasheet = False
print ('找到文件 -> {target} <- ..'.format(target = filename))
workbook = xlrd.open_workbook(filename)
for booksheet in workbook.sheets():
#print (line('发现一个sheet: ' + booksheet.name))
if booksheet.name in targetSheetName:
cursheet = booksheet.name
print ('找到 -> {target} <- sheet ..'.format(target = cursheet))
finddatasheet = True
ok = SheetParse(portion[0], booksheet)
fileName = portion[0]
else:
if finddatasheet:
print ('解析完成开始,开始分析结果.\n')
if ok == True:
if newfoloder == True:
fileOutput = open('./' + dirName + '/' + fileName + 'Data.lua.txt', 'w', encoding='utf-8')
else:
fileOutput = open('./' + fileName + 'Data.lua.txt', 'w', encoding='utf-8')
#print('writeData: ' + writeData)
fileOutput.write(writeData)
luaRequireData += line("require('LuaData." + fileName + "Data')")
else:
fileOutput = open('./' + dirName + '/' + fileName + 'Data.出错啦兄弟', 'w', encoding='utf-8')
if ok == False:
print ('当前 xls 生成失败.\n')
break
if newfoloder == True:
fileOutput = open('./' + dirName + '/' + luaRequirefileName, 'w', encoding='utf-8')
else:
fileOutput = open('./' + luaRequirefileName, 'w', encoding='utf-8')
fileOutput.write(luaRequireData)
writeData = ''
annotaionData = ''
header = False
else:
print ('没有找到 --> {target} <- sheet!生成失败..\n'.format(target = targetSheetName))
writeData = ''
annotaionData = ''
header = False