使用 Python 将excel 生成json

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

步骤:
  1. 安装 Pandas 和 OpenPyXL

    使用 pip 安装 pandasopenpyxl 库:

    复制代码
    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
相关推荐
金銀銅鐵4 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup118 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0010 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵12 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf13 小时前
Agent 流程编排
后端·python·agent
copyer_xyf13 小时前
Agent RAG
后端·python·agent
copyer_xyf13 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf14 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python