import xlrd, json, codecs
from collections import OrderedDict
wb = xlrd.open_workbook('code.xlsx') # 需要修改的转换的xlsx文件
convert_list = []
sh = wb.sheet_by_index(0)
title = sh.row_values(0) # 属性名
for row_num in range(1, sh.nrows):
row_v = sh.row_values(row_num) # 输出的是每一行的数据
single = OrderedDict() # 有序字典
for column in range(0, len(row_v)):
# print(title[colnum], rowvalue[colnum])
single[title[column]] = row_v[column]
convert_list.append(single)
with codecs.open('file.json', "w", "utf-8") as f:
f.write(json.dumps(convert_list, ensure_ascii=False, indent=2) + '\n')