import xlrd
import os
def readxlsx(xlsxname,type):
out=""
if(type=="list"):
out = "[";
if(os.path.exists(r'.\xlsx\\'+xlsxname+'.xlsx')):
read_xlrd = xlrd.open_workbook(r'.\xlsx\\'+xlsxname+'.xlsx')
sheet1 = read_xlrd.sheet_by_index(0)
sheet2 = read_xlrd.sheet_by_index(1);
if(sheet2.nrows>1):
top = sheet2.row_values(0)
for num in range(1, sheet2.nrows):
row = sheet2.row_values(num)
out += "{"
for topnum in range(0, sheet2.ncols):
out += "\n"
out += top[topnum]
out += ":"
value = row[topnum]
value_type = sheet1.cell_value(topnum + 1, 1)
if (value_type == "string"):
out += "'"
if(isinstance(value,str)):
out += value
else:
out+=str(value)
out += "'"
elif (value_type == "num"):
out += str(value)
elif (value_type == "list"):
out += readxlsx(value,"list")
elif(value_type=="object"):
out+=readxlsx(value,"object")
if (topnum < sheet2.ncols - 1):
out += ","
out += "\n"
out += "}"
if (num < sheet2.nrows - 1):
if (xlsxname != "first"):
out += ","
out += "\n"
if (type == "list"):
out += "]"
return out
if __name__=='__main__':
p= readxlsx("first","object")
fp=open("smartJson.json","w", encoding='utf-8')
fp.write(p)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
可以将xlsx中文件转换成json格式。点击bat文件运行。first.xlsx为入口文件。文件中sheet1表格定义数据名和格式(目前格式有num,string,list),sheet2 定义数据内容。注意:list格式的内容可以是数据,也可以是另一个xlsx文件名。xlsx文件都应在一个文件夹中。输入文件名为xlsxTojson.json(转换成普通json和转换成可导入小程序云开发中格式)
资源推荐
资源详情
资源评论
收起资源包目录
读取xlsx转换成json文件.rar (19个子文件)
读取xlsx转换成json文件
xlsxTojsonStart.bat 36B
smartJson.json 1.64MB
smartJson.py 2KB
smartJson.bat 37B
读取xlsx转换成json文件.rar 21KB
__init__.py 0B
.idea
misc.xml 294B
读取xlsx转换成json文件.iml 464B
modules.xml 317B
workspace.xml 7KB
inspectionProfiles
使用说明 81B
xlsxTojson.json 1.62MB
readxlsx.py 2KB
xlsx
first.xlsx 29KB
first0.xlsx 10KB
first2.xlsx 10KB
class.xlsx 31KB
first1.xlsx 10KB
~$first.xlsx 165B
共 19 条
- 1
资源评论
- qq_333963292021-03-27别下,毫无作用!
qq_14840239
- 粉丝: 6
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功