【原创实践】python 获取节假日列表 并保存为excel

要将给定的假期数据生成 Excel 文件,可以使用 Python 的 pandasopenpyxl 库来处理。下面是一个示例代码,展示如何将 JSON 数据转换为 Excel 文件。

数据来源

https://github.com/NateScarlet/holiday-cn/tree/master

安装依赖

首先,确保安装了 pandasopenpyxl。你可以使用以下命令来安装:

bash 复制代码
pip install pandas openpyxl

示例代码

以下是一个示例代码,展示如何将你的 JSON 数据转换为 Excel 文件:

python 复制代码
import pandas as pd
import json
from pandas import ExcelWriter


# 假期数据 JSON
holiday_data = {
    "$schema": "https://raw.githubusercontent.com/NateScarlet/holiday-cn/master/schema.json",
    "$id": "https://raw.githubusercontent.com/NateScarlet/holiday-cn/master/2024.json",
    "year": 2024,
    "papers": [
        "https://www.gov.cn/zhengce/zhengceku/202310/content_6911528.htm"
    ],
    "days": [
        {"name": "元旦", "date": "2024-01-01", "isOffDay": True},
        {"name": "春节", "date": "2024-02-04", "isOffDay": False},
        {"name": "春节", "date": "2024-02-10", "isOffDay": True},
        {"name": "春节", "date": "2024-02-11", "isOffDay": True},
        {"name": "春节", "date": "2024-02-12", "isOffDay": True},
        {"name": "春节", "date": "2024-02-13", "isOffDay": True},
        {"name": "春节", "date": "2024-02-14", "isOffDay": True},
        {"name": "春节", "date": "2024-02-15", "isOffDay": True},
        {"name": "春节", "date": "2024-02-16", "isOffDay": True},
        {"name": "春节", "date": "2024-02-17", "isOffDay": True},
        {"name": "春节", "date": "2024-02-18", "isOffDay": False},
        {"name": "清明节", "date": "2024-04-04", "isOffDay": True},
        {"name": "清明节", "date": "2024-04-05", "isOffDay": True},
        {"name": "清明节", "date": "2024-04-06", "isOffDay": True},
        {"name": "清明节", "date": "2024-04-07", "isOffDay": False},
        {"name": "劳动节", "date": "2024-04-28", "isOffDay": False},
        {"name": "劳动节", "date": "2024-05-01", "isOffDay": True},
        {"name": "劳动节", "date": "2024-05-02", "isOffDay": True},
        {"name": "劳动节", "date": "2024-05-03", "isOffDay": True},
        {"name": "劳动节", "date": "2024-05-04", "isOffDay": True},
        {"name": "劳动节", "date": "2024-05-05", "isOffDay": True},
        {"name": "劳动节", "date": "2024-05-11", "isOffDay": False},
        {"name": "端午节", "date": "2024-06-10", "isOffDay": True},
        {"name": "中秋节", "date": "2024-09-14", "isOffDay": False},
        {"name": "中秋节", "date": "2024-09-15", "isOffDay": True},
        {"name": "中秋节", "date": "2024-09-16", "isOffDay": True},
        {"name": "中秋节", "date": "2024-09-17", "isOffDay": True},
        {"name": "国庆节", "date": "2024-09-29", "isOffDay": False},
        {"name": "国庆节", "date": "2024-10-01", "isOffDay": True},
        {"name": "国庆节", "date": "2024-10-02", "isOffDay": True},
        {"name": "国庆节", "date": "2024-10-03", "isOffDay": True},
        {"name": "国庆节", "date": "2024-10-04", "isOffDay": True},
        {"name": "国庆节", "date": "2024-10-05", "isOffDay": True},
        {"name": "国庆节", "date": "2024-10-06", "isOffDay": True},
        {"name": "国庆节", "date": "2024-10-07", "isOffDay": True},
        {"name": "国庆节", "date": "2024-10-12", "isOffDay": False}
    ]
}


# 转换为 DataFrame
df = pd.DataFrame(holiday_data['days'])


# 更改表头
df.columns = ['假期名称', '日期', '是否休假']
df['是否休假'] = df['是否休假'].replace({True: '1', False: '0'})

# 显示 DataFrame
print(df)


# 保存为 Excel 文件
output_file = 'china_holidays_2024.xlsx'
with ExcelWriter(output_file, engine='openpyxl') as writer:
    df.to_excel(writer, sheet_name='Holidays', index=False)


print(f"Excel 文件已保存到 {output_file}")


print(f"Excel 文件已保存到 {output_file}")

代码说明

  1. 导入库 :导入 pandasjson
  2. 定义假期数据 :假期数据存储在 holiday_data 变量中。
  3. 转换为 DataFrame :使用 pd.DataFrame() 将假期数据转换为 Pandas DataFrame。
  4. 保存为 Excel 文件 :使用 df.to_excel() 将 DataFrame 保存为 Excel 文件。
  5. 打印消息:显示文件保存路径。

运行此代码后,将生成一个名为 china_holidays_2024.xlsx 的 Excel 文件,其中包含假期的详细信息。

相关推荐
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
倔强的石头_1 天前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程2 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪2 天前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook2 天前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田2 天前
使用 pkgutil 实现动态插件系统
python
前端付豪2 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽2 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img