实现使用python逐个读取当前文件夹的xls文档,并且读取每个文档里面的sheet内容,生成以sheet为目标的json文件。
注意:
1.主要使用模块:xlrd # http://pypi.python.org/pypi/xlrd
具体安装请自行搜索一下
2.经测试,不支持excel的新版本:xlsx,建设保存成旧版xls
目标文件格式:
第一行为命名,第二行开始数据
导出格式:
[
{"id":1, "name":"护盾", "power":200, "img_stone":"stone1", "img_buff":"buffShield", "value":5, "fly_speed":5, "move_pos":"0|100#-100|200#0|300#100|200", "keep_time":10},
{"id":2, "name":"回血", "power":200, "img_stone":"stone2", "img_buff":"buffBlood", "value":60, "fly_speed":5, "move_pos":"0|250#-250|350#0|450#250|350", "keep_time":10},
{"id":3, "name":"能量", "power":200, "img_stone":"stone1", "img_buff":"buffPower", "value":35, "fly_speed":5, "move_pos":"0|100#-100|200#0|300#100|201", "keep_time":10},
{"id":4, "name":"升级", "power":200, "img_stone":"stone2", "img_buff":"buffUpgrade", "value":1, "fly_speed":5, "move_pos":"0|250#-250|350#0|450#250|351", "keep_time":10},
{"id":5, "name":"灵敏", "power":200, "img_stone":"stone1", "img_buff":"buffSensitive", "value":5, "fly_speed":5, "move_pos":"0|100#-100|200#0|300#100|202", "keep_time":10}
{"id":6, "name":"视野", "power":200, "img_stone":"stone2", "img_buff":"buffEye", "value":3, "fly_speed":5, "move_pos":"0|250#-250|350#0|450#250|352", "keep_time":10}
]
文件目录:
python3 代码:
import os
import sys
import codecs
import xlrd # http://pypi.python.org/pypi/xlrd
def FloatToString(aFloat):
if type(aFloat) != float:
return ""
strTemp = str(aFloat)
strList = strTemp.split(".")
if len(strList) == 1:
return strTemp
else:
if strList[1] == "0":
return strList[0]
else:
return strTemp
def table2json(table, jsonfilename, fileDir):
hang = table.nrows
lie = table.ncols
f = codecs.open(fileDir+jsonfilename, "w", "utf-8")
f.write(u"[\n")
# print(f)
# 这里我们这的表是从第2行开始读
for r in range(1, hang):
# 每一行一组数据 一个对象的开始用{
print("参数:%s,总计%d,%a"%(r, hang, lie))
f.write(u" {")
for c in range(0, lie):
# 定义一个空的字符串
strCellValue = u""
# 获取一个单元格的值
CellObj = table.cell_value(r, c)
# print("获得:%s1,%s2"%(c, CellObj))
# 判断数据类型如果是float类型要转成字符串
if type(CellObj) == float:
# print("1获得float")
strCellValue = FloatToString(CellObj)
else:
# print("2获得不是float")
# 转成字符串
strCellValue = str(CellObj)
# 值里面写"在里面防止转义报错要去掉 做过滤
strCellValue = strCellValue.replace(u"\"", u"")
# 变成Json的值字符串要加"
strCellValue = u'\"'+strCellValue+u'\"'
strTmp = u"\"" + table.cell_value(0, c) + u"\":" + strCellValue
# print("获得strTmp:%s"%(strTmp))
# 如果不是最后一个需要加,
if c < lie-1:
strTmp += u", "
# 写字符串到{}中
f.write(strTmp)
f.write(u"}")
# 每一个对象后面要加,
if r < hang-2:
f.write(u",")
# 换行
f.write(u"\n")
# 最后所有的数据要用]反中括号包起来
f.write(u"]")
# 关闭文件
f.close()
print("转换完成表 ", jsonfilename)
return
# 取当前目录
curPath = os.path.dirname(__file__)
# 在当前目录下创建一个文件夹JSON
jsonDir = curPath+'\\JSON\\'
# 判断文件夹是否存在决定建不建文件夹
isExists = os.path.exists(jsonDir)
if not isExists:
os.makedirs(jsonDir)
# 遍历当前目录查询出所有的excel表
fileNameList = os.listdir(curPath)
print(fileNameList)
for a in fileNameList:
print(a)
# 这里只能读取xlsx的表 如果是其他的表请加入判断
extName = os.path.splitext(a)
# 剔除缓存的表
if(extName[0].find("~") >= 0 or extName[0].find("$") >= 0):
continue
# 只有这三种格式的才转 其他的不管
if(extName[1] == '.xlsx' or extName[1] == ".csv" or extName[1] == ".xls"):
# data = xlrd.open_workbook(curPath+'\\'+'item.xlsx')
data = xlrd.open_workbook(curPath+'\\'+a)
print("读取sheet名字:%s"%(data.sheet_names()))
for sheetName in data.sheet_names():
table = data.sheet_by_name(sheetName)
print("读取sheet获得table:%s"%(table))
table2json(table, sheetName+'.json', jsonDir)
# table = data.sheet_by_index(0)
# print("读取sheet获得table:%s"%(table))
# table2json(table, a.replace(".xlsx", "")+'.json', jsonDir)
print("所有的表转换完成")
优化建议:
1.第一个sheet(导航),定义每个sheet表的基本参数,比如:是否对应生成前后端,表名,有效行列数等;
2.sheet每个数据表第二、三行可以放备注,类型等