wjtx_conf_bt/ext_data/excel2json.py
2020-08-17 16:34:00 +08:00

51 lines
1.4 KiB
Python

# -*- coding: utf-8 -*-
import os
import json
import xlrd
DATA_ROW_INDEX = 5
DATA_COL_INDEX = 1
TYPE_ROW_INDEX = 2
NAME_ROW_INDEX = 4
def convertSheet(sourceFile, sheet):
data = []
for m in range(DATA_ROW_INDEX, sheet.nrows):
item = {}
for n in range(DATA_COL_INDEX, sheet.ncols):
field_name = sheet.row_values(NAME_ROW_INDEX)[n]
field_type = sheet.row_values(TYPE_ROW_INDEX)[n]
field_value = sheet.row_values(m)[n]
item[field_name] = field_value
if field_type == 'int':
item[field_name] = int(field_value)
elif field_type == 'string':
item[field_name] = str(field_value)
else:
assert False
#print(field_name, type_name, field_value)
#end for n
data.append(item)
#end for m
jsondata = json.dumps(data, indent=4, separators=(',', ': '), ensure_ascii=False)
out_file = open("./" + sourceFile[:-5] + ".json", "w")
out_file.write(jsondata)
out_file.close()
def convert(sourceFile):
if sourceFile != 'PlatformIds.xlsx':
return
print(sourceFile)
excel = xlrd.open_workbook(sourceFile)
for sheet in excel.sheets():
convertSheet(sourceFile, sheet)
break
def main():
for file in os.listdir('./'):
if file[0] != '~' and (file[-5:] == '.xlsx' or file[-5:] == '.xlsx'):
convert(file)
if __name__ == "__main__":
main()