Python生成日历导出Excel

python 复制代码
import openpyxl
from openpyxl.styles import Font, Alignment, Border, Side, PatternFill, NamedStyle
from openpyxl.utils import get_column_letter
from datetime import datetime, date, timedelta
from calendar import monthcalendar, month_abbr
import holidays
import os
# 创建中国节假日对象
cn_holidays = holidays.CN(years=2025)

# 创建工作簿
wb = openpyxl.Workbook()
wb.remove(wb.active)  # 删除默认sheet

# 定义样式
header_style = NamedStyle(name="header_style")
header_style.font = Font(bold=True, color="FFFFFF", size=12)
header_style.fill = PatternFill(start_color="4472C4", fill_type="solid")
header_style.alignment = Alignment(horizontal="center", vertical="center")
header_style.border = Border(left=Side(style='thin'), right=Side(style='thin'),
                             top=Side(style='thin'), bottom=Side(style='thin'))

weekend_style = NamedStyle(name="weekend_style")
weekend_style.font = Font(color="FF0000", bold=True)
weekend_style.fill = PatternFill(start_color="F2F2F2", fill_type="solid")

holiday_style = NamedStyle(name="holiday_style")
holiday_style.font = Font(color="FF0000", bold=True)
holiday_style.fill = PatternFill(start_color="FFF2CC", fill_type="solid")

normal_day_style = NamedStyle(name="normal_day_style")
normal_day_style.alignment = Alignment(horizontal="center", vertical="center")
normal_day_style.border = Border(left=Side(style='thin'), right=Side(style='thin'),
                                 top=Side(style='thin'), bottom=Side(style='thin'))

# 添加样式到工作簿
wb.add_named_style(header_style)
wb.add_named_style(weekend_style)
wb.add_named_style(holiday_style)
wb.add_named_style(normal_day_style)

# 为每个月创建日历
for month in range(1, 13):
    month_title = f"{month_abbr[month]}"
    ws = wb.create_sheet(title=month_title)

    # 设置月份标题
    ws.merge_cells('A1:G1')
    ws['A1'] = f"{month_title}"
    ws['A1'].style = header_style

    # 设置星期标题
    weekdays = ["一", "二", "三", "四", "五", "六", "日"]
    for col, day in enumerate(weekdays, start=1):
        cell = ws.cell(row=3, column=col, value=day)
        cell.style = header_style
        ws.column_dimensions[get_column_letter(col)].width = 12

    # 获取当月日历
    cal = monthcalendar(2025, month)

    # 填充日期
    for week_num, week in enumerate(cal, start=4):
        for day_num, day in enumerate(week, start=1):
            if day != 0:
                current_date = date(2025, month, day)
                cell = ws.cell(row=week_num, column=day_num, value=day)

                # 判断是否是周末
                if day_num in [6, 7]:  # 星期六、星期日
                    cell.style = weekend_style
                else:
                    cell.style = normal_day_style

                # 判断是否是节假日
                if current_date in cn_holidays:
                    cell.style = holiday_style
                    holiday_name = cn_holidays.get(current_date)
                    ws.cell(row=week_num, column=day_num, value=f"{day}\n{holiday_name}").alignment = Alignment(
                        horizontal="center", vertical="center", wrap_text=True)

    # 添加备注说明
    ws['A10'] = "图例说明:"
    ws['A11'] = "红色: 周末"
    ws['A11'].style = weekend_style
    ws['A12'] = "黄色: 法定节假日"
    ws['A12'].style = holiday_style



# 创建目标目录(如果不存在)
os.makedirs("../file", exist_ok=True)

# 保存文件到指定路径
filename = "../file/2025年日历.xlsx"
wb.save(filename)
print(f"2025年日历已生成,文件名: {filename}")
相关推荐
拧之几秒前
✅XXL-JOB的基本使用
java
迈火24 分钟前
PuLID_ComfyUI:ComfyUI中的图像生成强化插件
开发语言·人工智能·python·深度学习·计算机视觉·stable diffusion·语音识别
老朋友此林1 小时前
MongoDB GEO 项目场景 ms-scope 实战
java·数据库·spring boot·mongodb
华仔啊2 小时前
前端必看!12个JS神级简写技巧,代码效率直接飙升80%,告别加班!
前端·javascript
excel2 小时前
dep.ts 逐行解读
前端·javascript·vue.js
爱上妖精的尾巴2 小时前
5-20 WPS JS宏 every与some数组的[与或]迭代(数组的逻辑判断)
开发语言·前端·javascript·wps·js宏·jsa
excel2 小时前
Vue3 响应式核心源码全解析:Dep、Link 与 track/trigger 完整执行机制详解
前端
前端大卫3 小时前
一个关于时区的线上问题
前端·javascript·vue.js
浔川python社3 小时前
《网络爬虫技术规范与应用指南系列》(xc—5)完
爬虫·python
豆沙沙包?3 小时前
2025年--Lc165--H637.二叉树的层平均值(二叉树的层序遍历)--Java版
java·开发语言