基于python的日历生成器(用于打印)

最近发现用平板越来越少了,上次充电还是两周前,今天打开一看还有一半电,每天打开平板就用用苹果日历,感觉用这个日历做计划很好用。所以打算把平板卖掉买个挂历。
每个月都用A4纸打印当月的日历,写写计划什么的,很好用。但苦于没有好用的的日历,遂写了个脚本,输入年月就能生成当月的日历,每一天空间大一些,可以写写计划挂墙上类似于下图是2024年2月的日历。
在这里插入图片描述
先pip一下用到的库,内存很小的。

pip install docx
pip install lunardate 
pip install calendar

批注很详细,直接把代码粘下面了

from docx import Document
from docx.shared import Cm
from lunardate import LunarDate 
import calendar

def create_calendar(year, month):
    # 计算该月的天数和第一天是星期几
    first_day, num_days = calendar.monthrange(year, month)
    first_day += 1
    # print("num_days:", num_days,"first_day:",first_day)
    # 创建一个空的日历表格
    calendar_month = [[''] * 7 for _ in range(6)]  # 6行7列的日历布局

    # 填充日历表格
    day = 1
    for week in range(6):  # 每周
        for day_of_week in range(7):  # 每天一周
            if day > num_days:
                break
            if (week == 0 and day_of_week < first_day):
                calendar_month[week][day_of_week] = ''
            else:
                calendar_month[week][day_of_week] = str(day)
                day += 1

    # 统计日历每一行的个数,检查空行并直接删除行
    for week in calendar_month:
        num_of_day_week = 0
        for day_of_week in week:
            if day_of_week != '':
                num_of_day_week += 1
        if num_of_day_week == 0:
            calendar_month.pop()
        del num_of_day_week

    return calendar_month

def create_word_document(year, month):
    # 创建一个新的Word文档
    doc = Document()

    # 农历用中文表示
    month_CN = ["一","二","三","四","五","六","七","八","九","十","十一","十二"]
    day_CN = ["初一","初二","初三","初四","初五","初六","初七","初八","初九","初十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十","三一"]
    lunar_months_fest = [['春节', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '元宵', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                          [0, '龙抬头', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                          [0, 0, 0, 0, '端午', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                          [0, 0, 0, 0, 0, 0, '七夕', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '中秋', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                          [0, 0, 0, 0, 0, 0, 0, 0, '重阳', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] # 大行不顾细谨
    ad_months_fest = [['元旦', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '情人', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                        [0, 0, 0, 0, '清明', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                        ['劳动', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
                        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                        ['国庆', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '平安夜', '圣诞', 0, 0, 0, 0, 0, 0]]

    # 获取日历数据
    calendar_month = create_calendar(year, month)
    table = doc.add_table(rows=1, cols=7)
    hdr_cells = table.rows[0].cells
    
    # 填充表格头部(星期)
    hdr_cells[0].text = 'Sun'
    hdr_cells[1].text = 'Mon'
    hdr_cells[2].text = 'Tue'
    hdr_cells[3].text = 'Wed'
    hdr_cells[4].text = 'Thu'
    hdr_cells[5].text = 'Fri'
    hdr_cells[6].text = 'Sat'

    week_of_month = 1
    for week in calendar_month:
        
        # 有新的周就加一行
        row = table.add_row()
        
        for day_of_week, day in enumerate(week):
            # 设置单元格高度,docx库里没有设置行高的接口,只能one by one,尽量占满A4纸,有些月份跨度5、6个星期
            if len(calendar_month) == 5:
                table.rows[week_of_month].height = Cm(4.0)
            else:
                table.rows[week_of_month].height = Cm(3.0)

            # 填充表格内容
            if day:
                cell = table.rows[week_of_month].cells[day_of_week]
                
                # 查一下对应的农历日期
                lunar_date = LunarDate.fromSolarDate(int(year), int(month), int(day))
                lunar_day = lunar_date.day
                lunar_month = lunar_date.month
                
                # 日期内容插入表格 农历节日 公历节日 农历月初
                if lunar_months_fest[lunar_month - 1][lunar_day -1] != 0 :
                    cell.text = str(day) + lunar_months_fest[lunar_month - 1][lunar_day -1]
               
                elif ad_months_fest[int(month) - 1][int(day) - 1] != 0 :
                    cell.text = str(day) + ad_months_fest[int(month) - 1][int(day) - 1] 

                elif lunar_day == 1:
                    cell.text = str(day) + month_CN[lunar_month - 1] +"月"

                else:
                    cell.text = str(day) + day_CN[lunar_day - 1]

        week_of_month += 1

    # 保存文档
    filename = "C:\\\\Users\\\\SJJ\\\\Desktop\\\\" + f"Calendar_{year}_{month}.docx"
    doc.save(filename)
    print(f"Document '{filename}' created successfully.")

# 使用函数创建Word文档
create_word_document(2024, 2)  # 更改这里以生成不同年月的日历

over

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值