Python100个库分享第32个—chinese_calendar选定日期标记法定节假日

专栏导读

在这里插入图片描述

  • 🌸 欢迎来到Python办公自动化专栏—Python处理办公问题,解放您的双手

  • 🏳️‍🌈 博客主页:请点击——> 一晌小贪欢的博客主页求关注

  • 👍 该系列文章专栏:请点击——>Python办公自动化专栏求订阅

  • 🕷 此外还有爬虫专栏:请点击——>Python爬虫基础专栏求订阅

  • 📕 此外还有python基础专栏:请点击——>Python基础学习专栏求订阅

  • 文章作者技术和水平有限,如果文中出现错误,希望大家能指正🙏

  • ❤️ 欢迎各位佬关注! ❤️

库的介绍

  • 要在代码中实现检查日期是否为休息日(如周六、周日或法定节假日),

  • 并相应地标注它们,可以使用 chinese_calendar 库。

  • 这个库提供了中国节假日的查询功能并能帮助您判断任意日期是否为工作日、周末或节假日。

库的安装

pip install chinese_calenda -i https://pypi.tuna.tsinghua.edu.cn/simple/

完整代码

import tkinter as tk
from tkinter import ttk, scrolledtext
from tkcalendar import DateEntry
from datetime import timedelta
from chinese_calendar import is_holiday, is_workday, get_holiday_detail


def generate_dates():
    start_date = start_cal.get_date()
    end_date = end_cal.get_date()
    if start_date and end_date:
        delta = end_date - start_date
        time_list = []

        text_widget.config(state=tk.NORMAL)
        text_widget.delete('1.0', tk.END)

        for i in range(delta.days + 1):
            date = start_date + timedelta(days=i)
            date_str = date.strftime("%Y-%m-%d")

            # 检查是否为休息日
            if not is_workday(date):
                if date.weekday() == 5:  # 周六
                    time_list.append(f"{date_str} 休息(周六)")
                elif date.weekday() == 6:  # 周日
                    time_list.append(f"{date_str} 休息(周日)")
                else:  # 法定节假日
                    holiday_name = get_holiday_detail(date)
                    time_list.append(f"{date_str} 法定节假日:{holiday_name}")
            else:
                time_list.append(date_str)

                # 插入新日期列表
        for date_info in time_list:
            text_widget.insert(tk.END, date_info + "\n")

        text_widget.config(state=tk.DISABLED)
    else:
        text_widget.config(state=tk.NORMAL)
        text_widget.delete('1.0', tk.END)
        text_widget.insert(tk.END, "Please select both start and end dates.\n")
        text_widget.config(state=tk.DISABLED)
        # 创建主窗口


root = tk.Tk()
root.title("Date Range Generator")

# 创建并打包小部件
start_label = ttk.Label(root, text="Start Date:")
start_label.pack()

start_cal = DateEntry(root, width=12, background='darkgray', foreground='white', selectbackground='lightblue')
start_cal.pack()

end_label = ttk.Label(root, text="End Date:")
end_label.pack()

end_cal = DateEntry(root, width=12, background='darkgray', foreground='white', selectbackground='lightblue')
end_cal.pack()

generate_button = ttk.Button(root, text="Generate Dates", command=generate_dates)
generate_button.pack()

# 使用ScrolledText代替Text并自动添加滚动条
text_widget = scrolledtext.ScrolledText(root, state=tk.DISABLED, height=20, width=60)
text_widget.pack()

# 启动GUI事件循环
root.mainloop()

总结

  • 希望对初学者有帮助

  • 致力于办公自动化的小小程序员一枚

  • 希望能得到大家的【一个免费关注】!感谢

  • 求个 🤞 关注 🤞

  • 此外还有办公自动化专栏,欢迎大家订阅:Python办公自动化专栏

  • 求个 ❤️ 喜欢 ❤️

  • 此外还有爬虫专栏,欢迎大家订阅:Python爬虫基础专栏

  • 求个 👍 收藏 👍

  • 此外还有Python基础专栏,欢迎大家订阅:Python基础学习专栏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一晌小贪欢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值