基于Python生成橫向年度日歷的代碼實現(xiàn)
更新時間:2026年01月28日 09:05:36 作者:U盤失蹤了
文章介紹了使用Python的calendar模塊和openpyxl庫生成Excel日歷的方法,并描述了如何實現(xiàn)動態(tài)列寬、嚴格對齊和樣式統(tǒng)一,以生成美觀專業(yè)的日歷,需要的朋友可以參考下
技術選型
calendar模塊:Python 內置模塊,用于獲取每個月的天數(shù)和日期,不用自己計算平年閏年。openpyxl庫:用于創(chuàng)建和編輯 Excel 文件,支持合并單元格、設置樣式等操作,生成的文件可直接在 WPS/Excel 中打開。
核心思路
- 動態(tài)列寬:每個月的標題合并列數(shù)等于該月的實際天數(shù)(如 1 月 31 列、2 月 28 列),徹底解決空白單元格問題。
- 嚴格對齊:標題和日期的起始列完全一致,保證視覺上嚴絲合縫。
- 樣式統(tǒng)一:設置統(tǒng)一的字體和對齊方式,讓生成的日歷更美觀專業(yè)。
import calendar
from openpyxl import Workbook
from openpyxl.styles import Alignment, Font
from openpyxl.utils import get_column_letter
def generate_aligned_calendar(target_year):
"""
生成標題與日期嚴格對齊的橫向年度日歷
核心修復:每個月標題合并的列數(shù) = 該月實際天數(shù),無空白單元格
"""
wb = Workbook()
ws = wb.active
ws.title = f"{target_year}年日歷"
# 定義樣式
title_font = Font(bold=True, size=12)
title_align = Alignment(horizontal='center', vertical='center')
date_align = Alignment(horizontal='right', vertical='center')
current_col = 1 # 記錄當前起始列,動態(tài)累加
for month in range(1, 13):
# 獲取當前月份的實際天數(shù)(關鍵:用實際天數(shù)代替固定31)
days_in_month = calendar.monthrange(target_year, month)[1]
end_col = current_col + days_in_month - 1
# 1. 寫入并合并月份標題(合并列數(shù) = 該月天數(shù))
month_title = f"{month}月"
ws.merge_cells(start_row=1, start_column=current_col,
end_row=1, end_column=end_col)
title_cell = ws.cell(row=1, column=current_col, value=month_title)
title_cell.font = title_font
title_cell.alignment = title_align
# 2. 寫入日期(從當前列開始,連續(xù)寫入1-月末)
for day in range(1, days_in_month + 1):
date_cell = ws.cell(row=2, column=current_col + day - 1, value=day)
date_cell.alignment = date_align
# 更新下一個月的起始列
current_col = end_col + 1
# 統(tǒng)一調整列寬
for col in range(1, current_col):
ws.column_dimensions[get_column_letter(col)].width = 2.5
# 保存文件
file_name = f"{target_year}年對齊版日歷.xlsx"
wb.save(file_name)
print(f"? {target_year}年日歷生成成功!文件:{file_name}")
print(f"?? 修復效果:標題與日期嚴格對齊,無空白單元格")
# 調用示例:生成2026年日歷
if __name__ == "__main__":
generate_aligned_calendar(2026)效果圖

| 需求 | 角色 | 人員 | 工時 | 1月 | ||||||||||||||||||||||||||||||
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | ||||
| 登錄功能 | 項目經理 | 小強 | 0 | |||||||||||||||||||||||||||||||
| 產品經理 | 小明 | 1 | 1 | |||||||||||||||||||||||||||||||
| 開發(fā) | 小龍 | 1 | 1 | |||||||||||||||||||||||||||||||
| 測試 | 小王 | 0.3 | 0.3 | |||||||||||||||||||||||||||||||
| 注冊功能 | 項目經理 | 小強 | 0 | |||||||||||||||||||||||||||||||
| 產品經理 | 小明 | 0 | ||||||||||||||||||||||||||||||||
| 開發(fā) | 小龍 | 0 | ||||||||||||||||||||||||||||||||
| 測試 | 小王 | 0 | ||||||||||||||||||||||||||||||||
| 注銷功能 | 項目經理 | 小強 | 0 | |||||||||||||||||||||||||||||||
| 產品經理 | 小明 | 0 | ||||||||||||||||||||||||||||||||
| 開發(fā) | 小龍 | 0 | ||||||||||||||||||||||||||||||||
| 測試 | 小王 | 0 | ||||||||||||||||||||||||||||||||
到此這篇關于基于Python生成橫向年度日歷的代碼實現(xiàn)的文章就介紹到這了,更多相關Python生成橫向年度日歷內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于Python+Tkinter開發(fā)一個表格比對和&整理工具
日常辦公和數(shù)據(jù)處理中,Excel表格是高頻使用的工具,本文將使用Python和Tkinter開發(fā)一個表格比對和&整理工具,感興趣的小伙伴可以了解下2025-12-12
Python中的魔法方法__repr__和__str__用法實例詳解
這篇文章主要介紹了Python中的__repr__和__str__方法,它們分別用于提供對象的官方字符串表示和用戶友好的字符串表示,通過重寫這兩個方法,可以自定義對象的打印輸出,文中通過代碼將用法介紹的非常詳細,需要的朋友可以參考下2025-02-02
Pytorch實現(xiàn)將模型的所有參數(shù)的梯度清0
這篇文章主要介紹了Pytorch實現(xiàn)將模型的所有參數(shù)的梯度清0,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
一文了解Python中NotImplementedError的作用
NotImplementedError是一個內置異常類,本文主要介紹了一文了解Python中NotImplementedError的作用,具有一定的參考價值,感興趣的可以了解一下2024-03-03

