python 实现excel转化成json文件

时间:2021-01-21 22:16:24

1.准备工作

  1. python 2.7 安装
  2. 安装xlrd -- pip install xlrd

2. 直接上代码

import xlrd
from collections import OrderedDict
import json
import codecs wb = xlrd.open_workbook('file.xlsx') convert_list = []
sh = wb.sheet_by_index(0)
title = sh.row_values(0)
for rownum in range(1, sh.nrows):
rowvalue = sh.row_values(rownum)
single = OrderedDict()
for colnum in range(0, len(rowvalue)):
print(title[colnum], rowvalue[colnum])
single[title[colnum]] = rowvalue[colnum]
convert_list.append(single) j = json.dumps(convert_list) with codecs.open('file.json',"w","utf-8") as f:
f.write(j)