-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_json.py
148 lines (122 loc) · 3.3 KB
/
make_json.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
#!/usr/bin/env python
# coding=utf-8
import os
import config
import excel_reader
import json_writer
import sys
conf = config.Config('excel_conf.conf')
def get_to_list(reader, key, _list):
reader.sel_col(0)
reader.sel_row(key)
value = reader.get()
_list.append(value)
while True:
value = reader.next_col()
if not value:
break
_list.append(value)
def gen_line(reader, writer, line, key_tab, type_tab):
key = reader.get_col_row(conf.key_col, line)
if not key:
return
writer.table_beg(key, type_tab[conf.key_col])
reader.sel_col(0)
reader.sel_row(line)
value = reader.get()
if value != None:
writer.attribute(key_tab[0], value, type_tab[0])
for idx in range(1, reader.get_ncols()):
key = key_tab[idx]
value = reader.next_col()
_type = type_tab[idx]
if value != None:
writer.attribute(key, value, _type)
writer.table_end()
def gen_sheet(reader, writer):
key_tab = list()
type_tab = list()
conf.key_row = int(conf.key_row)
conf.type_row = int(conf.type_row)
conf.key_col = int(conf.key_col)
get_to_list(reader, conf.key_row, key_tab)
get_to_list(reader, conf.type_row, type_tab)
assert(key_tab[conf.key_col] and type_tab[conf.key_col] and len(key_tab) == len(type_tab))
for line in range(int(conf.start_row), reader.get_nrows()):
gen_line(reader, writer, line, key_tab, type_tab)
del key_tab
del type_tab
def make_style_1(reader, writer):
reader.sel_sheet(0)
gen_sheet(reader, writer)
def make_style_2(reader, writer):
for idx in range(reader.len_sheet()):
reader.sel_sheet(idx)
writer.table_beg(reader.sheet_name(), 'str')
gen_sheet(reader, writer)
writer.table_end()
def make_global_config(reader, writer):
_tab = 0
reader.sel_sheet(_tab)
for _idx in range(reader.len_sheet()):
_sname = reader.sheet_name()
writer.table_beg(_sname, 'str')
_line = 2
reader.sel_row(_line)
for idx in range(2, reader.get_ncols()):
reader.sel_col(0)
_key = reader.get()
reader.sel_col(1)
_type = reader.get()
reader.sel_col(2)
_value = reader.get()
if not _key or not _type or not _value:
break
writer.attribute(_key, _value, _type)
_line += 1
reader.sel_row(_line)
writer.table_end()
_tab += 1
reader.sel_sheet(_tab)
def make_error_code(reader, writer):
reader.sel_sheet(0)
_line = 1
reader.sel_row(_line)
for idx in range(1, reader.get_ncols()):
reader.sel_col(2)
_value = reader.get()
reader.sel_col(1)
_key = reader.get()
if not _key or not _value:
break
writer.attribute(_key, _value, 'str')
_line += 1
reader.sel_row(_line)
for filename in os.listdir(conf.inpath):
if filename.startswith('~$'):
continue
check = False
fullinpath = conf.inpath + filename
fulloutpath = conf.outpath + filename
if not os.path.isfile(conf.inpath + filename):
continue
for exname in conf.exnames:
if filename.find(exname) != -1:
check = True
break
if not check:
continue
reader = excel_reader.Excel_reader(fullinpath)
writer = json_writer.json_writer(fulloutpath)
print('translate:\n' + fullinpath + '\noutput file:\n' + fulloutpath + '\n\n')
writer.write_beg()
if filename == 'System.xlsx':
make_global_config(reader, writer)
elif filename == 'ErrorCode.xlsx':
make_error_code(reader, writer)
else:
eval('make_style_' + conf.style)(reader, writer)
writer.write_end()
del reader
del writer
print('excel2json finished!')