使用 Python 将excel 生成json

Python 的 pandas 库是一个强大的数据处理工具,可以轻松地将 Excel 文件转换为 JSON 格式。

步骤:
  1. 安装 Pandas 和 OpenPyXL
    使用 pip 安装 pandas 和 openpyxl 库:

    pip install pandas openpyxl
  2. 编写 Python 脚本
    创建一个 Python 脚本,例如 excel_to_json.py,内容如下:

    import pandas as pd
    
    def excel_to_json(excel_file, sheet_name, json_file):
        # 读取 Excel 文件
        df = pd.read_excel(excel_file, sheet_name=sheet_name)
        
        # 将 DataFrame 转换为 JSON
        df.to_json(json_file, orient='records', force_ascii=False)
    
    # 示例用法
    excel_to_json('input.xlsx', 'Sheet1', 'output.json')
  3. 运行脚本
    在终端中运行脚本:

    python excel_to_json.py

你可能感兴趣的:(python,python,excel,json)