使用 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
相关推荐
AI创界者6 小时前
Python 进阶实战(七十章):遗留系统重构、渐进式绞杀与技术债治理实战
人工智能·python·aigc
ZhengEnCi7 小时前
电脑操作 Agent 一直点不中:截图没更新时,提示词该怎么改才能停止瞎试
人工智能·python·agent
Wellijohn9 小时前
dict、tuple、dataclass 还是 slots?我把 6 种数据载体的 8 项开销全测了一遍
后端·python·性能优化
bufengwangluo10 小时前
爬虫和调用 API 有什么区别?为什么优先选 API
python·网络爬虫·ip·api调用
Dmz丶10 小时前
前端转 AI 100 天 Day 54:RAG 项目实战(三)——多轮对话 + 会话管理
python
console.log('npc')10 小时前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习
geovindu11 小时前
python:Logistic Regression
开发语言·后端·python·算法
geovindu13 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
真上帝的左手13 小时前
2. 编程语言-Python-Anaconda
开发语言·python