-
Notifications
You must be signed in to change notification settings - Fork 2
/
conv2lua.py
87 lines (69 loc) · 2.15 KB
/
conv2lua.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
import os,sys,getopt
import xlrd
##################################################################
def convert_sheet(sheet, output_path):
nrows = sheet.nrows
ncols = sheet.ncols
keys = {}
for i in range(0, ncols):
keys[i] = sheet.cell(0, i).value
output = []
output.append("Config = {")
for i in range(1, nrows):
output.append(" {")
for j in keys:
val = sheet.cell(i, j).value
if val == '' or val == u'':
continue
line = " %s = %s" % (keys[j], val)
line = ((j == ncols - 1) and line or ("%s, " % line)).encode("utf-8")
output.append(line)
output.append((i == nrows - 1) and " }" or " },")
output.append("}")
name = '%s/%s.lua' % (output_path, sheet.name)
try:
fd = open(name, 'w')
except Exception, e:
print str(e)
return
output = [line + '\n' for line in output]
fd.writelines(output)
fd.close()
print 'convert %s success' % name
##################################################################
def convert(xls, output_path):
try:
book = xlrd.open_workbook(xls)
except Exception, e:
print str(e)
return
# get sheet one by one
for sheet in book.sheets():
convert_sheet(sheet, output_path)
##################################################################
def usage():
print '''
conv2lua.py usage:
-f, --excel_file <excel file name> [required]
-O, --output_path <convert result's path> [optional]
'''
quit()
##################################################################
if __name__=="__main__":
sopt = 'f:O:'
lopt = ['excel_file=', 'output_path=']
try:
opts, args = getopt.getopt(sys.argv[1:], sopt, lopt)
except Exception, e:
print str(e)
usage()
excel = ''
output_path = '.'
for opt, val in opts:
if opt in ('-f', '--excel_file'):
excel = val
elif opt in ('-O', '--output_path'):
output_path = val
if excel == '':
usage()
convert(excel, output_path)