美文网首页
Python ExcelToJson beta

Python ExcelToJson beta

作者: 码农老谭 | 来源:发表于2019-10-11 17:02 被阅读0次

将Excel转成Json格式数据

# -*- coding:utf-8 -*-
import xlrd
import json
import codecs
import os
import ConfigParser

#把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))
            json_file_name = sheet
        # 获取单个表单编号
            sheet = book.sheet_by_index(int(worksheets.index(sheet)))
            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(),json_file_name,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+os.path.sep+file_name+".json",'w',"utf-8")
    output.write(data)
    output.close()
if __name__ == '__main__':
    PROPERTIES_FILE_PTAH='file.properties'
    config = ConfigParser.RawConfigParser()
    config.read(PROPERTIES_FILE_PTAH)
    excel_file_name=config.get('ExcelToJsonConfig', 'excel.file.name')
    json_data=Excel2Json(excel_file_name)

file.properties

[ExcelToJsonConfig]
excel.file.name=打包参数表V210.xls

配置文件中是excel文件名,将会根据表格当中的表单生成对应的Json文件,一个表单一个JSON文件

相关文章

网友评论

      本文标题:Python ExcelToJson beta

      本文链接:https://www.haomeiwen.com/subject/mokxmctx.html