Excel2Json.py3

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xlrd
import json
import os
import sys

outPath = "./"

def readFromExcel(filepath):
    dicts = {}
    wr = xlrd.open_workbook(filepath)
    # sheet1 = wr.sheet_names()[0]
    sheet1 = wr.sheet_by_index(0) #获取第一个表格中的第一个sheet
    rows = sheet1.nrows
    cols = sheet1.ncols
    print("--------",rows)
    for i in range(1, rows):
        key_row = str(sheet1.cell(i, 0).value)
        # if key not in dicts:
        dicts[key_row] = {}
        for j in range(1, cols):
            key_col = str(sheet1.cell(0, j).value)
            dicts[key_row][key_col] = sheet1.cell(i, j).value.replace("\\n", "\n")
    return dicts

def write_json(dicts, filename):
    print("----------")
    with open(filename, "w+") as fo:
        fo.write(json.dumps(dicts, indent= 4, sort_keys=True, ensure_ascii=False))
        

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("not have dest file, please fill in")
        exit(0)
    filename = sys.argv[1]
    print("------filename", filename)
    filepath = os.path.join("./", filename)
    if not os.path.isfile(filepath):
        print("file is not exits")
        exit(0)
    else:
        dicts = readFromExcel(filepath)
    if dicts:
        arr = filename.split("/")
        excelname = arr[len(arr) - 1].split(".")[0] + ".json"
        write_json(dicts, excelname)

    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Last login: Fri Jul 14 16:46:34 on ttys000 jeongyiii@Jeongyiii-2 ~ % cd Downloads jeongyiii@Jeongyiii-2 Downloads % python3 json2xml.py Traceback (most recent call last): File "json2xml.py", line 56, in <module> process_json_file(file_path) File "json2xml.py", line 50, in process_json_file dataframe_to_excel(expanded_df, excel_file_path) File "json2xml.py", line 32, in dataframe_to_excel writer.save() AttributeError: 'OpenpyxlWriter' object has no attribute 'save' jeongyiii@Jeongyiii-2 Downloads % vim json2xml.py jeongyiii@Jeongyiii-2 Downloads % python3 json2xml.py Excel file generated: example.xlsx jeongyiii@Jeongyiii-2 Downloads % python3 json2xml.py Excel file generated: example.xlsx jeongyiii@Jeongyiii-2 Downloads % python3 json2xml.py Excel file generated: example.xlsx jeongyiii@Jeongyiii-2 Downloads % vim json2xml.py jeongyiii@Jeongyiii-2 Downloads % python3 json2xml.py Excel file generated: example.xlsx jeongyiii@Jeongyiii-2 Downloads % python3 json2xml.py Excel file generated: example.xlsx jeongyiii@Jeongyiii-2 Downloads % vim json2xml.py jeongyiii@Jeongyiii-2 Downloads % vim json2xml.py def process_json_file(file_path): # Read JSON file json_data = read_json_file(file_path) # Parse JSON object json_obj = json.loads(json_data) # Convert JSON data to DataFrame df = json_to_dataframe(json_obj) # Expand the 'template' column expanded_df = expand_template_column(df) # Write to Excel file excel_file_path = file_path.replace('.json', '.xlsx') dataframe_to_excel(expanded_df, excel_file_path) print("Excel file generated:", excel_file_path) # Usage example file_path = 'example.json' process_json_file(file_path) 说明
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值