python转json到excel_python实现 Excel 转为json

# -*- coding:utf-8 -*-

# @Date : 2016-07-13 14:47:56

# @Function: 把Excel表单转化为json文件

# @Author : JennyZhang

import xlrd

import json

import codecs

import os

#把excel表格中指定sheet转为json

def Excel2Json(file_path):

#打开excel文件

if get_data(file_path) is not None:

book = get_data(file_path)

#抓取所有sheet页的名称

worksheets = book.sheet_names()

print "该Excel包含的表单列表为:\n"

for sheet in worksheets:

print ('%s,%s' %(worksheets.index(sheet),sheet))

inp = raw_input(u'请输入表单名对应的编号,对应表单将自动转为json:\n')

sheet = book.sheet_by_index(int(inp))

row_0 = sheet.row(0) #第一行是表单标题

nrows=sheet.nrows #行号

ncols=sheet.ncols #列号

result={} #定义json对象

result["title"]=file_path #表单标题

result["rows"]=nrows #行号

result["children"]=[] #每一行作为数组的一项

#遍历所有行,将excel转化为json对象

for i in range(nrows):

if i==0:

continue

tmp={}

#遍历当前行所有列

for j in range(ncols):

#获取当前列中文标题

title_de=str(row_0[j]).decode('unicode_escape')

title_cn= title_de.split("'")[1]

#获取单元格的值

tmp[title_cn]=sheet.row_values(i)[j]

result["children"].append(tmp)

json_data=json.dumps(result,indent= 4,sort_keys=True).decode('unicode_escape')

saveFile(os.getcwd(),worksheets[int(inp)],json_data)

print json_data

# 获取excel数据源

def get_data(file_path):

"""获取excel数据源"""

try:

data = xlrd.open_workbook(file_path)

return data

except Exception, e:

print u'excel表格读取失败:%s' %e

return None

def saveFile(file_path,file_name,data):

output = codecs.open(file_path+"/"+file_name+".json",'w',"utf-8")

output.write(data)

output.close()

if __name__ == '__main__':

file_path = raw_input(u'请输入excel文件路径:\n')

json_data=Excel2Json(file_path)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值