【办公类-53-09】20260131Python模仿制作2025学年第二学期校历(excel+pdf)

背景需求:

今天是放假一天,把原来的代码找出来修改一下

【办公类-53-05】20250701Python模仿制作2025学年第一学期校历(excel+pdf)https://mp.csdn.net/mp_blog/creation/editor/149054656

网上的校历信息

起止日期

月份颜色不同安排

2026的国定放假安排

代码展示

python 复制代码
'''
模仿制作2026年2月第二学期的校历(标注节假日)3月第一天是年月日,每月第1天是月日,其他都是日
星火讯飞、deepseek修订、阿夏
20260131
'''

from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
import datetime
import time, os
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.page import PageMargins
import win32com.client  # 用于 Excel 转 PDF

path = r'C:\Users\jg2yXRZ\OneDrive\桌面\20260301校历'


title = '2025学年第二学期校历(阿夏20260201)'
m = 0  # 起始日期前空几天(周一开始,就是空0天)

# 创建一个新的Excel工作簿
workbook = Workbook()
sheet = workbook.active

# 设置页边距(单位为厘米)
left_margin_cm = 1.3
right_margin_cm = 1.3
top_margin_cm = 2
bottom_margin_cm = 2

# 将厘米转换为英寸
left_margin_inch = left_margin_cm * 0.393701
right_margin_inch = right_margin_cm * 0.393701
top_margin_inch = top_margin_cm * 0.393701
bottom_margin_inch = bottom_margin_cm * 0.393701

# 设置页边距
margins = PageMargins(left=left_margin_inch, right=right_margin_inch, 
                      top=top_margin_inch, bottom=bottom_margin_inch)
sheet.page_margins = margins

# 设置标题行
title_row = ["周次", "一", "二", "三", "四", "五", "六", "日"]
sheet.append(title_row)

# 设置日期范围
start_date = datetime.date(2026, 3, 2)
end_date = datetime.date(2026, 6, 30)

# 生成日期列表并包含标题行和空格行
date_list = [title_row]
for i in range(m):
    date_list.append([''])  # 添加一个空格行

current_day = start_date
while current_day <= end_date:
    date_list.append([current_day])
    current_day += datetime.timedelta(days=1)

# 计算周数和将日期列表写入Excel
current_week = 1
for i in range(1, len(date_list), 7):
    week_dates = date_list[i:i+7]
    
    # 在A列中添加周次
    sheet.cell(row=current_week + 1, column=1).value = f"第{current_week}周"
    cell = sheet.cell(row=current_week + 1, column=1)
    cell.alignment = Alignment(horizontal='center', vertical='center')
    cell.font = Font(size=16)
    # 添加第一列的边框
    thin_border = Border(
        left=Side(style='thin', color='000000'),
        right=Side(style='thin', color='000000'),
        top=Side(style='thin', color='000000'),
        bottom=Side(style='thin', color='000000')
    )
    cell.border = thin_border
    
    # 在正确的单元格中添加日期
    for j in range(7):  # 确保遍历7天(即使数据不足7天)
        cell = sheet.cell(row=current_week + 1, column=j + 2)
        
        # 如果当前日期存在,则填充数据
        if j < len(week_dates) and week_dates[j]:
            date_obj = week_dates[j][0]
            
            # 设置日期显示格式
            # 第一天20260302
            if date_obj.year == 2026 and date_obj.month == 3 and date_obj.day == 2:
                cell.value = f"{date_obj.year}/{date_obj.month}/{date_obj.day}"
         
            # 如果是每月第一天,就填写月日
            elif date_obj.day == 1:
                cell.value = f"{date_obj.month}/{date_obj.day}"       
                 # 劳动节假期(汉字) 是四月1日,需要年月 
                
                if date_obj.year == 2026 and date_obj.month == 5 and date_obj.day == 1:
                    cell.value = f"{date_obj.month}/{date_obj.day}劳动"         
                            
             
            else:
                cell.value = f"{date_obj.day}"
                # 清明节假期(汉字)不是4月1日,就只要写日    
                if date_obj.year == 2026 and date_obj.month == 4 and date_obj.day in range(4, 7):
                    cell.value = f"{date_obj.day}清明"   

                 # 劳动节假期(汉字)5月2-5日,就只要写日
                elif date_obj.year == 2026 and date_obj.month == 5 and date_obj.day in range(2, 6):
                    cell.value = f"{date_obj.day}劳动"
               
                 # 端午节假期(汉字)    
                elif date_obj.year == 2026 and date_obj.month == 6 and date_obj.day in range(19, 22):
                    cell.value = f"{date_obj.day}端午"
            
            # 特殊日期处理(国庆节和元旦)填充白色
            is_special_date = False

             # 清明节假期 (4月4-6日)
            if date_obj.month == 4 and 4 <= date_obj.day <= 6:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅橘色
            
            # 劳动节假期 (5月1-5日)
            if date_obj.month == 5 and 1 <= date_obj.day <= 5:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅红色

            # 端午假期 (6月19-21日)
            if date_obj.month == 6 and 19 <= date_obj.day <= 21:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅红色
                

           
                
            # 周六周日调休日 (5月9日劳动节周六上班)
            # elif (date_obj.month == 9 and date_obj.day == 28) or \
            #      (date_obj.month == 10 and date_obj.day == 11) or \
            #      (date_obj.month == 1 and date_obj.day == 4):            
                # is_special_date = True
                # if date_obj.month == 9:
                #     cell.fill = PatternFill(start_color="FFFFCC", end_color="FFFFCC", fill_type="solid")  # 浅黄
                # elif date_obj.month == 10:
                #     cell.fill = PatternFill(start_color="FFCCCC", end_color="FFCCCC", fill_type="solid")  # 浅红
                # elif date_obj.month == 1:
                #     cell.fill = PatternFill(start_color="FFCC99", end_color="FFCC99", fill_type="solid")  # 浅橘
            
              # 周六周日调休日 (5月9日劳动节周六上班)
            elif (date_obj.month == 5 and date_obj.day == 9):
                is_special_date = True
                if date_obj.month == 5:
                    cell.fill = PatternFill(start_color="CCCCCC", end_color="CCCCCC", fill_type="solid")  # 浅灰色

            # 如果不是特殊日期,则按月份设置颜色
            if not is_special_date:
                # 周末默认白色,除非是特殊日期
                if j >= 5:  # 周六(第6列)和周日(第7列)
                    cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 白色
                else:
                    # 按月份设置颜色
                    # if date_obj.month == 2:
                    #     cell.fill = PatternFill(start_color="FFFFCC", end_color="FFFFCC", fill_type="solid")  # 浅黄色
                    if date_obj.month == 3:
                        cell.fill = PatternFill(start_color="FFCCCC", end_color="FFCCCC", fill_type="solid")  # 浅红色
                    elif date_obj.month == 4:
                        cell.fill = PatternFill(start_color="CCFFCC", end_color="CCFFCC", fill_type="solid")  # 浅绿色
                    elif date_obj.month == 5:
                        cell.fill = PatternFill(start_color="CCCCCC", end_color="CCCCCC", fill_type="solid")  # 浅灰色
                    elif date_obj.month == 6:
                        cell.fill = PatternFill(start_color="FFCC99", end_color="FFCC99", fill_type="solid")  # 浅橘色
        
        # 无论是否有数据,都设置边框
        thin_border = Border(
            left=Side(style='thin', color='000000'),
            right=Side(style='thin', color='000000'),
            top=Side(style='thin', color='000000'),
            bottom=Side(style='thin', color='000000')
        )
        cell.border = thin_border
        cell.alignment = Alignment(horizontal='center', vertical='center')
        cell.font = Font(size=16)
    
    current_week += 1

# 设置标题行样式
for col in range(1, len(title_row) + 1):
    cell = sheet.cell(row=1, column=col)
    cell.alignment = Alignment(horizontal='center', vertical='center')
    thin_border = Border(
        left=Side(style='thin', color='000000'),
        right=Side(style='thin', color='000000'),
        top=Side(style='thin', color='000000'),
        bottom=Side(style='thin', color='000000')
    )
    cell.border = thin_border
    cell.fill = PatternFill(start_color="ADD8E6", end_color="ADD8E6", fill_type="solid")  # 浅蓝色
    cell.font = Font(size=16)

# 设置行高和列宽
for row in range(1, sheet.max_row + 2):
    sheet.row_dimensions[row].height = 30

# 特别确保最后一行高度为30
sheet.row_dimensions[sheet.max_row].height = 30

for col in range(1, sheet.max_column + 1):
    column_letter = get_column_letter(col)
    sheet.column_dimensions[column_letter].width = 11.5

# 添加标题行
sheet.insert_rows(1)
sheet.merge_cells('A1:H1')
sheet.cell(row=1, column=1).value = title
sheet.cell(row=1, column=1).alignment = Alignment(horizontal='center', vertical='center')
sheet.cell(row=1, column=1).font = Font(size=18, bold=True)
sheet.cell(row=1, column=1).fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid") 

# 保存工作簿
excel_file = path + fr"\00-03 {title}.xlsx"
workbook.save(excel_file)
print(f"校历已生成: {excel_file}")

# ========== 转换为 PDF ==========
def excel_to_pdf(input_excel_path, output_pdf_path):
    try:
        # 启动 Excel 应用程序
        excel = win32com.client.Dispatch("Excel.Application")
        excel.Visible = False  # 不显示 Excel 界面
        
        # 打开 Excel 文件
        workbook = excel.Workbooks.Open(input_excel_path)
        
        # 设置打印区域(整个工作表)
        sheet = workbook.ActiveSheet
        sheet.PageSetup.Zoom = False
        sheet.PageSetup.FitToPagesWide = 1  # 适应宽度到 1 页
        sheet.PageSetup.FitToPagesTall = False  # 高度不限制
        
        # 导出为 PDF
        workbook.ExportAsFixedFormat(0, output_pdf_path)  # 0 表示 PDF 格式
        
        # 关闭 Excel
        workbook.Close(False)
        excel.Quit()
        
        print(f"PDF 已生成: {output_pdf_path}")
    except Exception as e:
        print(f"转换失败: {e}")

# 调用转换函数
pdf_file = path + fr"\00-03 {title}.pdf"
excel_to_pdf(excel_file, pdf_file)

time.sleep(2)

我觉得这份"日"版的大部分格子都只有一个"日",还要查询"月"(看色块的第一个),有点烦,我还是做一个"月日"版

python 复制代码
'''
模仿制作2026年2月第二学期的校历(标注节假日)3月第一天是年月日,其他都是月日
星火讯飞、deepseek修订、阿夏
20260131
'''

from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
import datetime
import time, os
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.page import PageMargins
import win32com.client  # 用于 Excel 转 PDF

path = r'C:\Users\jg2yXRZ\OneDrive\桌面\20260301校历'


title = '2025学年第二学期校历(阿夏20260201)'
m = 0  # 起始日期前空几天(周一开始,就是空0天)

# 创建一个新的Excel工作簿
workbook = Workbook()
sheet = workbook.active

# 设置页边距(单位为厘米)
left_margin_cm = 1.3
right_margin_cm = 1.3
top_margin_cm = 2
bottom_margin_cm = 2

# 将厘米转换为英寸
left_margin_inch = left_margin_cm * 0.393701
right_margin_inch = right_margin_cm * 0.393701
top_margin_inch = top_margin_cm * 0.393701
bottom_margin_inch = bottom_margin_cm * 0.393701

# 设置页边距
margins = PageMargins(left=left_margin_inch, right=right_margin_inch, 
                      top=top_margin_inch, bottom=bottom_margin_inch)
sheet.page_margins = margins

# 设置标题行
title_row = ["周次", "一", "二", "三", "四", "五", "六", "日"]
sheet.append(title_row)

# 设置日期范围
start_date = datetime.date(2026, 3, 2)
end_date = datetime.date(2026, 6, 30)

# 生成日期列表并包含标题行和空格行
date_list = [title_row]
for i in range(m):
    date_list.append([''])  # 添加一个空格行

current_day = start_date
while current_day <= end_date:
    date_list.append([current_day])
    current_day += datetime.timedelta(days=1)

# 计算周数和将日期列表写入Excel
current_week = 1
for i in range(1, len(date_list), 7):
    week_dates = date_list[i:i+7]
    
    # 在A列中添加周次
    sheet.cell(row=current_week + 1, column=1).value = f"第{current_week}周"
    cell = sheet.cell(row=current_week + 1, column=1)
    cell.alignment = Alignment(horizontal='center', vertical='center')
    cell.font = Font(size=16)
    # 添加第一列的边框
    thin_border = Border(
        left=Side(style='thin', color='000000'),
        right=Side(style='thin', color='000000'),
        top=Side(style='thin', color='000000'),
        bottom=Side(style='thin', color='000000')
    )
    cell.border = thin_border
    
    # 在正确的单元格中添加日期
    for j in range(7):  # 确保遍历7天(即使数据不足7天)
        cell = sheet.cell(row=current_week + 1, column=j + 2)
        
        # 如果当前日期存在,则填充数据
        if j < len(week_dates) and week_dates[j]:
            date_obj = week_dates[j][0]
            
            # 设置日期显示格式
            # 第一天20260302
            if date_obj.year == 2026 and date_obj.month == 3 and date_obj.day == 2:
                cell.value = f"{date_obj.year}/{date_obj.month}/{date_obj.day}"
         
            # 如果是除了第一天是年月日,其他都是月日
            else:
            #  date_obj.day == 1:
                cell.value = f"{date_obj.month}/{date_obj.day}"       
                 # 劳动节假期(汉字) 是四月1日,需要年月 
                
                if date_obj.year == 2026 and date_obj.month == 5 and date_obj.day == 1:
                    cell.value = f"{date_obj.month}/{date_obj.day}劳动"         
                            
             
            # else:
            #     cell.value = f"{date_obj.day}"
                # 清明节假期(汉字)不是4月1日,就只要写日    
                if date_obj.year == 2026 and date_obj.month == 4 and date_obj.day in range(4, 7):
                    cell.value = f"{date_obj.month}/{date_obj.day}清明"   

                 # 劳动节假期(汉字)5月2-5日,就只要写日
                elif date_obj.year == 2026 and date_obj.month == 5 and date_obj.day in range(2, 6):
                    cell.value = f"{date_obj.month}/{date_obj.day}劳动"
               
                 # 端午节假期(汉字)    
                elif date_obj.year == 2026 and date_obj.month == 6 and date_obj.day in range(19, 22):
                    cell.value = f"{date_obj.month}/{date_obj.day}端午"
            
            # 特殊日期处理(国庆节和元旦)填充白色
            is_special_date = False

             # 清明节假期 (4月4-6日)
            if date_obj.month == 4 and 4 <= date_obj.day <= 6:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅橘色
            
            # 劳动节假期 (5月1-5日)
            if date_obj.month == 5 and 1 <= date_obj.day <= 5:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅红色

            # 端午假期 (6月19-21日)
            if date_obj.month == 6 and 19 <= date_obj.day <= 21:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅红色
                

           
                
            # 周六周日调休日 (5月9日劳动节周六上班)
            # elif (date_obj.month == 9 and date_obj.day == 28) or \
            #      (date_obj.month == 10 and date_obj.day == 11) or \
            #      (date_obj.month == 1 and date_obj.day == 4):            
                # is_special_date = True
                # if date_obj.month == 9:
                #     cell.fill = PatternFill(start_color="FFFFCC", end_color="FFFFCC", fill_type="solid")  # 浅黄
                # elif date_obj.month == 10:
                #     cell.fill = PatternFill(start_color="FFCCCC", end_color="FFCCCC", fill_type="solid")  # 浅红
                # elif date_obj.month == 1:
                #     cell.fill = PatternFill(start_color="FFCC99", end_color="FFCC99", fill_type="solid")  # 浅橘
            
              # 周六周日调休日 (5月9日劳动节周六上班)
            elif (date_obj.month == 5 and date_obj.day == 9):
                is_special_date = True
                if date_obj.month == 5:
                    cell.fill = PatternFill(start_color="CCCCCC", end_color="CCCCCC", fill_type="solid")  # 浅灰色

            # 如果不是特殊日期,则按月份设置颜色
            if not is_special_date:
                # 周末默认白色,除非是特殊日期
                if j >= 5:  # 周六(第6列)和周日(第7列)
                    cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 白色
                else:
                    # 按月份设置颜色
                    # if date_obj.month == 2:
                    #     cell.fill = PatternFill(start_color="FFFFCC", end_color="FFFFCC", fill_type="solid")  # 浅黄色
                    if date_obj.month == 3:
                        cell.fill = PatternFill(start_color="FFCCCC", end_color="FFCCCC", fill_type="solid")  # 浅红色
                    elif date_obj.month == 4:
                        cell.fill = PatternFill(start_color="CCFFCC", end_color="CCFFCC", fill_type="solid")  # 浅绿色
                    elif date_obj.month == 5:
                        cell.fill = PatternFill(start_color="CCCCCC", end_color="CCCCCC", fill_type="solid")  # 浅灰色
                    elif date_obj.month == 6:
                        cell.fill = PatternFill(start_color="FFCC99", end_color="FFCC99", fill_type="solid")  # 浅橘色
        
        # 无论是否有数据,都设置边框
        thin_border = Border(
            left=Side(style='thin', color='000000'),
            right=Side(style='thin', color='000000'),
            top=Side(style='thin', color='000000'),
            bottom=Side(style='thin', color='000000')
        )
        cell.border = thin_border
        cell.alignment = Alignment(horizontal='center', vertical='center')
        cell.font = Font(size=16)
    
    current_week += 1

# 设置标题行样式
for col in range(1, len(title_row) + 1):
    cell = sheet.cell(row=1, column=col)
    cell.alignment = Alignment(horizontal='center', vertical='center')
    thin_border = Border(
        left=Side(style='thin', color='000000'),
        right=Side(style='thin', color='000000'),
        top=Side(style='thin', color='000000'),
        bottom=Side(style='thin', color='000000')
    )
    cell.border = thin_border
    cell.fill = PatternFill(start_color="ADD8E6", end_color="ADD8E6", fill_type="solid")  # 浅蓝色
    cell.font = Font(size=16)

# 设置行高和列宽
for row in range(1, sheet.max_row + 2):
    sheet.row_dimensions[row].height = 30

# 特别确保最后一行高度为30
sheet.row_dimensions[sheet.max_row].height = 30

for col in range(1, sheet.max_column + 1):
    column_letter = get_column_letter(col)
    sheet.column_dimensions[column_letter].width = 11.5

# 添加标题行
sheet.insert_rows(1)
sheet.merge_cells('A1:H1')
sheet.cell(row=1, column=1).value = title
sheet.cell(row=1, column=1).alignment = Alignment(horizontal='center', vertical='center')
sheet.cell(row=1, column=1).font = Font(size=18, bold=True)
sheet.cell(row=1, column=1).fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid") 

# 保存工作簿
excel_file = path + fr"\00-03 {title}.xlsx"
workbook.save(excel_file)
print(f"校历已生成: {excel_file}")

# ========== 转换为 PDF ==========
def excel_to_pdf(input_excel_path, output_pdf_path):
    try:
        # 启动 Excel 应用程序
        excel = win32com.client.Dispatch("Excel.Application")
        excel.Visible = False  # 不显示 Excel 界面
        
        # 打开 Excel 文件
        workbook = excel.Workbooks.Open(input_excel_path)
        
        # 设置打印区域(整个工作表)
        sheet = workbook.ActiveSheet
        sheet.PageSetup.Zoom = False
        sheet.PageSetup.FitToPagesWide = 1  # 适应宽度到 1 页
        sheet.PageSetup.FitToPagesTall = False  # 高度不限制
        
        # 导出为 PDF
        workbook.ExportAsFixedFormat(0, output_pdf_path)  # 0 表示 PDF 格式
        
        # 关闭 Excel
        workbook.Close(False)
        excel.Quit()
        
        print(f"PDF 已生成: {output_pdf_path}")
    except Exception as e:
        print(f"转换失败: {e}")

# 调用转换函数
pdf_file = path + fr"\00-03 {title}.pdf"
excel_to_pdf(excel_file, pdf_file)

time.sleep(2)

因为加了月的数字,表格宽度不够宽,会有遮挡,

宽度改成13.5

用豆包提取每周的5天(通常是周一、周五,为了做周计划)

数据贴到I列,并修改一下(第10周从周二到周六)

为了区分两份差别,修改题目名称

python 复制代码
'''
模仿制作2026年2月第二学期的校历(标注节假日)3月第一天是年月日,每月第1天是月日,其他都是日
星火讯飞、deepseek修订、阿夏
20260131
'''

from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
import datetime
import time, os
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.page import PageMargins
import win32com.client  # 用于 Excel 转 PDF

path = r'C:\Users\jg2yXRZ\OneDrive\桌面\20260301校历'


title = '2025学年第二学期校历"日"版(阿夏20260201)'
m = 0  # 起始日期前空几天(周一开始,就是空0天)

# 创建一个新的Excel工作簿
workbook = Workbook()
sheet = workbook.active

# 设置页边距(单位为厘米)
left_margin_cm = 1.3
right_margin_cm = 1.3
top_margin_cm = 2
bottom_margin_cm = 2

# 将厘米转换为英寸
left_margin_inch = left_margin_cm * 0.393701
right_margin_inch = right_margin_cm * 0.393701
top_margin_inch = top_margin_cm * 0.393701
bottom_margin_inch = bottom_margin_cm * 0.393701

# 设置页边距
margins = PageMargins(left=left_margin_inch, right=right_margin_inch, 
                      top=top_margin_inch, bottom=bottom_margin_inch)
sheet.page_margins = margins

# 设置标题行
title_row = ["周次", "一", "二", "三", "四", "五", "六", "日"]
sheet.append(title_row)

# 设置日期范围
start_date = datetime.date(2026, 3, 2)
end_date = datetime.date(2026, 6, 30)

# 生成日期列表并包含标题行和空格行
date_list = [title_row]
for i in range(m):
    date_list.append([''])  # 添加一个空格行

current_day = start_date
while current_day <= end_date:
    date_list.append([current_day])
    current_day += datetime.timedelta(days=1)

# 计算周数和将日期列表写入Excel
current_week = 1
for i in range(1, len(date_list), 7):
    week_dates = date_list[i:i+7]
    
    # 在A列中添加周次
    sheet.cell(row=current_week + 1, column=1).value = f"第{current_week}周"
    cell = sheet.cell(row=current_week + 1, column=1)
    cell.alignment = Alignment(horizontal='center', vertical='center')
    cell.font = Font(size=16)
    # 添加第一列的边框
    thin_border = Border(
        left=Side(style='thin', color='000000'),
        right=Side(style='thin', color='000000'),
        top=Side(style='thin', color='000000'),
        bottom=Side(style='thin', color='000000')
    )
    cell.border = thin_border
    
    # 在正确的单元格中添加日期
    for j in range(7):  # 确保遍历7天(即使数据不足7天)
        cell = sheet.cell(row=current_week + 1, column=j + 2)
        
        # 如果当前日期存在,则填充数据
        if j < len(week_dates) and week_dates[j]:
            date_obj = week_dates[j][0]
            
            # 设置日期显示格式
            # 第一天20260302
            if date_obj.year == 2026 and date_obj.month == 3 and date_obj.day == 2:
                cell.value = f"{date_obj.year}/{date_obj.month}/{date_obj.day}"
         
            # 如果是每月第一天,就填写月日
            elif date_obj.day == 1:
                cell.value = f"{date_obj.month}/{date_obj.day}"       
                 # 劳动节假期(汉字) 是四月1日,需要年月 
                
                if date_obj.year == 2026 and date_obj.month == 5 and date_obj.day == 1:
                    cell.value = f"{date_obj.month}/{date_obj.day}劳动"         
                            
             
            else:
                cell.value = f"{date_obj.day}"
                # 清明节假期(汉字)不是4月1日,就只要写日    
                if date_obj.year == 2026 and date_obj.month == 4 and date_obj.day in range(4, 7):
                    cell.value = f"{date_obj.day}清明"   

                 # 劳动节假期(汉字)5月2-5日,就只要写日
                elif date_obj.year == 2026 and date_obj.month == 5 and date_obj.day in range(2, 6):
                    cell.value = f"{date_obj.day}劳动"
               
                 # 端午节假期(汉字)    
                elif date_obj.year == 2026 and date_obj.month == 6 and date_obj.day in range(19, 22):
                    cell.value = f"{date_obj.day}端午"
            
            # 特殊日期处理(国庆节和元旦)填充白色
            is_special_date = False

             # 清明节假期 (4月4-6日)
            if date_obj.month == 4 and 4 <= date_obj.day <= 6:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅橘色
            
            # 劳动节假期 (5月1-5日)
            if date_obj.month == 5 and 1 <= date_obj.day <= 5:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅红色

            # 端午假期 (6月19-21日)
            if date_obj.month == 6 and 19 <= date_obj.day <= 21:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅红色
                

           
                
            # 周六周日调休日 (5月9日劳动节周六上班)
            # elif (date_obj.month == 9 and date_obj.day == 28) or \
            #      (date_obj.month == 10 and date_obj.day == 11) or \
            #      (date_obj.month == 1 and date_obj.day == 4):            
                # is_special_date = True
                # if date_obj.month == 9:
                #     cell.fill = PatternFill(start_color="FFFFCC", end_color="FFFFCC", fill_type="solid")  # 浅黄
                # elif date_obj.month == 10:
                #     cell.fill = PatternFill(start_color="FFCCCC", end_color="FFCCCC", fill_type="solid")  # 浅红
                # elif date_obj.month == 1:
                #     cell.fill = PatternFill(start_color="FFCC99", end_color="FFCC99", fill_type="solid")  # 浅橘
            
              # 周六周日调休日 (5月9日劳动节周六上班)
            elif (date_obj.month == 5 and date_obj.day == 9):
                is_special_date = True
                if date_obj.month == 5:
                    cell.fill = PatternFill(start_color="CCCCCC", end_color="CCCCCC", fill_type="solid")  # 浅灰色

            # 如果不是特殊日期,则按月份设置颜色
            if not is_special_date:
                # 周末默认白色,除非是特殊日期
                if j >= 5:  # 周六(第6列)和周日(第7列)
                    cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 白色
                else:
                    # 按月份设置颜色
                    # if date_obj.month == 2:
                    #     cell.fill = PatternFill(start_color="FFFFCC", end_color="FFFFCC", fill_type="solid")  # 浅黄色
                    if date_obj.month == 3:
                        cell.fill = PatternFill(start_color="FFCCCC", end_color="FFCCCC", fill_type="solid")  # 浅红色
                    elif date_obj.month == 4:
                        cell.fill = PatternFill(start_color="CCFFCC", end_color="CCFFCC", fill_type="solid")  # 浅绿色
                    elif date_obj.month == 5:
                        cell.fill = PatternFill(start_color="CCCCCC", end_color="CCCCCC", fill_type="solid")  # 浅灰色
                    elif date_obj.month == 6:
                        cell.fill = PatternFill(start_color="FFCC99", end_color="FFCC99", fill_type="solid")  # 浅橘色
        
        # 无论是否有数据,都设置边框
        thin_border = Border(
            left=Side(style='thin', color='000000'),
            right=Side(style='thin', color='000000'),
            top=Side(style='thin', color='000000'),
            bottom=Side(style='thin', color='000000')
        )
        cell.border = thin_border
        cell.alignment = Alignment(horizontal='center', vertical='center')
        cell.font = Font(size=16)
    
    current_week += 1

# 设置标题行样式
for col in range(1, len(title_row) + 1):
    cell = sheet.cell(row=1, column=col)
    cell.alignment = Alignment(horizontal='center', vertical='center')
    thin_border = Border(
        left=Side(style='thin', color='000000'),
        right=Side(style='thin', color='000000'),
        top=Side(style='thin', color='000000'),
        bottom=Side(style='thin', color='000000')
    )
    cell.border = thin_border
    cell.fill = PatternFill(start_color="ADD8E6", end_color="ADD8E6", fill_type="solid")  # 浅蓝色
    cell.font = Font(size=16)

# 设置行高和列宽
for row in range(1, sheet.max_row + 2):
    sheet.row_dimensions[row].height = 30

# 特别确保最后一行高度为30
sheet.row_dimensions[sheet.max_row].height = 30

for col in range(1, sheet.max_column + 1):
    column_letter = get_column_letter(col)
    sheet.column_dimensions[column_letter].width = 11.5

# 添加标题行
sheet.insert_rows(1)
sheet.merge_cells('A1:H1')
sheet.cell(row=1, column=1).value = title
sheet.cell(row=1, column=1).alignment = Alignment(horizontal='center', vertical='center')
sheet.cell(row=1, column=1).font = Font(size=18, bold=True)
sheet.cell(row=1, column=1).fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid") 

# 保存工作簿
excel_file = path + fr"\00-03 {title}.xlsx"
workbook.save(excel_file)
print(f"校历已生成: {excel_file}")

# ========== 转换为 PDF ==========
def excel_to_pdf(input_excel_path, output_pdf_path):
    try:
        # 启动 Excel 应用程序
        excel = win32com.client.Dispatch("Excel.Application")
        excel.Visible = False  # 不显示 Excel 界面
        
        # 打开 Excel 文件
        workbook = excel.Workbooks.Open(input_excel_path)
        
        # 设置打印区域(整个工作表)
        sheet = workbook.ActiveSheet
        sheet.PageSetup.Zoom = False
        sheet.PageSetup.FitToPagesWide = 1  # 适应宽度到 1 页
        sheet.PageSetup.FitToPagesTall = False  # 高度不限制
        
        # 导出为 PDF
        workbook.ExportAsFixedFormat(0, output_pdf_path)  # 0 表示 PDF 格式
        
        # 关闭 Excel
        workbook.Close(False)
        excel.Quit()
        
        print(f"PDF 已生成: {output_pdf_path}")
    except Exception as e:
        print(f"转换失败: {e}")

# 调用转换函数
pdf_file = path + fr"\00-03 {title}.pdf"
excel_to_pdf(excel_file, pdf_file)

time.sleep(2)
python 复制代码
'''
模仿制作2026年2月第二学期的校历(标注节假日)3月第一天是年月日,其他都是月日
星火讯飞、deepseek修订、阿夏
20260131
'''

from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
import datetime
import time, os
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.page import PageMargins
import win32com.client  # 用于 Excel 转 PDF

path = r'C:\Users\jg2yXRZ\OneDrive\桌面\20260301校历'


title = '2025学年第二学期校历"月日"版(阿夏20260201)'
m = 0  # 起始日期前空几天(周一开始,就是空0天)

# 创建一个新的Excel工作簿
workbook = Workbook()
sheet = workbook.active

# 设置页边距(单位为厘米)
left_margin_cm = 1.3
right_margin_cm = 1.3
top_margin_cm = 2
bottom_margin_cm = 2

# 将厘米转换为英寸
left_margin_inch = left_margin_cm * 0.393701
right_margin_inch = right_margin_cm * 0.393701
top_margin_inch = top_margin_cm * 0.393701
bottom_margin_inch = bottom_margin_cm * 0.393701

# 设置页边距
margins = PageMargins(left=left_margin_inch, right=right_margin_inch, 
                      top=top_margin_inch, bottom=bottom_margin_inch)
sheet.page_margins = margins

# 设置标题行
title_row = ["周次", "一", "二", "三", "四", "五", "六", "日"]
sheet.append(title_row)

# 设置日期范围
start_date = datetime.date(2026, 3, 2)
end_date = datetime.date(2026, 6, 30)

# 生成日期列表并包含标题行和空格行
date_list = [title_row]
for i in range(m):
    date_list.append([''])  # 添加一个空格行

current_day = start_date
while current_day <= end_date:
    date_list.append([current_day])
    current_day += datetime.timedelta(days=1)

# 计算周数和将日期列表写入Excel
current_week = 1
for i in range(1, len(date_list), 7):
    week_dates = date_list[i:i+7]
    
    # 在A列中添加周次
    sheet.cell(row=current_week + 1, column=1).value = f"第{current_week}周"
    cell = sheet.cell(row=current_week + 1, column=1)
    cell.alignment = Alignment(horizontal='center', vertical='center')
    cell.font = Font(size=16)
    # 添加第一列的边框
    thin_border = Border(
        left=Side(style='thin', color='000000'),
        right=Side(style='thin', color='000000'),
        top=Side(style='thin', color='000000'),
        bottom=Side(style='thin', color='000000')
    )
    cell.border = thin_border
    
    # 在正确的单元格中添加日期
    for j in range(7):  # 确保遍历7天(即使数据不足7天)
        cell = sheet.cell(row=current_week + 1, column=j + 2)
        
        # 如果当前日期存在,则填充数据
        if j < len(week_dates) and week_dates[j]:
            date_obj = week_dates[j][0]
            
            # 设置日期显示格式
            # 第一天20260302
            if date_obj.year == 2026 and date_obj.month == 3 and date_obj.day == 2:
                cell.value = f"{date_obj.year}/{date_obj.month}/{date_obj.day}"
         
            # 如果是除了第一天是年月日,其他都是月日
            else:
            #  date_obj.day == 1:
                cell.value = f"{date_obj.month}/{date_obj.day}"       
                 # 劳动节假期(汉字) 是四月1日,需要年月 
                
                if date_obj.year == 2026 and date_obj.month == 5 and date_obj.day == 1:
                    cell.value = f"{date_obj.month}/{date_obj.day}劳动"         
                            
             
            # else:
            #     cell.value = f"{date_obj.day}"
                # 清明节假期(汉字)不是4月1日,就只要写日    
                if date_obj.year == 2026 and date_obj.month == 4 and date_obj.day in range(4, 7):
                    cell.value = f"{date_obj.month}/{date_obj.day}清明"   

                 # 劳动节假期(汉字)5月2-5日,就只要写日
                elif date_obj.year == 2026 and date_obj.month == 5 and date_obj.day in range(2, 6):
                    cell.value = f"{date_obj.month}/{date_obj.day}劳动"
               
                 # 端午节假期(汉字)    
                elif date_obj.year == 2026 and date_obj.month == 6 and date_obj.day in range(19, 22):
                    cell.value = f"{date_obj.month}/{date_obj.day}端午"
            
            # 特殊日期处理(国庆节和元旦)填充白色
            is_special_date = False

             # 清明节假期 (4月4-6日)
            if date_obj.month == 4 and 4 <= date_obj.day <= 6:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅橘色
            
            # 劳动节假期 (5月1-5日)
            if date_obj.month == 5 and 1 <= date_obj.day <= 5:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅红色

            # 端午假期 (6月19-21日)
            if date_obj.month == 6 and 19 <= date_obj.day <= 21:
                is_special_date = True
                cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 浅红色
                

           
                
            # 周六周日调休日 (5月9日劳动节周六上班)
            # elif (date_obj.month == 9 and date_obj.day == 28) or \
            #      (date_obj.month == 10 and date_obj.day == 11) or \
            #      (date_obj.month == 1 and date_obj.day == 4):            
                # is_special_date = True
                # if date_obj.month == 9:
                #     cell.fill = PatternFill(start_color="FFFFCC", end_color="FFFFCC", fill_type="solid")  # 浅黄
                # elif date_obj.month == 10:
                #     cell.fill = PatternFill(start_color="FFCCCC", end_color="FFCCCC", fill_type="solid")  # 浅红
                # elif date_obj.month == 1:
                #     cell.fill = PatternFill(start_color="FFCC99", end_color="FFCC99", fill_type="solid")  # 浅橘
            
              # 周六周日调休日 (5月9日劳动节周六上班)
            elif (date_obj.month == 5 and date_obj.day == 9):
                is_special_date = True
                if date_obj.month == 5:
                    cell.fill = PatternFill(start_color="CCCCCC", end_color="CCCCCC", fill_type="solid")  # 浅灰色

            # 如果不是特殊日期,则按月份设置颜色
            if not is_special_date:
                # 周末默认白色,除非是特殊日期
                if j >= 5:  # 周六(第6列)和周日(第7列)
                    cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")  # 白色
                else:
                    # 按月份设置颜色
                    # if date_obj.month == 2:
                    #     cell.fill = PatternFill(start_color="FFFFCC", end_color="FFFFCC", fill_type="solid")  # 浅黄色
                    if date_obj.month == 3:
                        cell.fill = PatternFill(start_color="FFCCCC", end_color="FFCCCC", fill_type="solid")  # 浅红色
                    elif date_obj.month == 4:
                        cell.fill = PatternFill(start_color="CCFFCC", end_color="CCFFCC", fill_type="solid")  # 浅绿色
                    elif date_obj.month == 5:
                        cell.fill = PatternFill(start_color="CCCCCC", end_color="CCCCCC", fill_type="solid")  # 浅灰色
                    elif date_obj.month == 6:
                        cell.fill = PatternFill(start_color="FFCC99", end_color="FFCC99", fill_type="solid")  # 浅橘色
        
        # 无论是否有数据,都设置边框
        thin_border = Border(
            left=Side(style='thin', color='000000'),
            right=Side(style='thin', color='000000'),
            top=Side(style='thin', color='000000'),
            bottom=Side(style='thin', color='000000')
        )
        cell.border = thin_border
        cell.alignment = Alignment(horizontal='center', vertical='center')
        cell.font = Font(size=16)
    
    current_week += 1

# 设置标题行样式
for col in range(1, len(title_row) + 1):
    cell = sheet.cell(row=1, column=col)
    cell.alignment = Alignment(horizontal='center', vertical='center')
    thin_border = Border(
        left=Side(style='thin', color='000000'),
        right=Side(style='thin', color='000000'),
        top=Side(style='thin', color='000000'),
        bottom=Side(style='thin', color='000000')
    )
    cell.border = thin_border
    cell.fill = PatternFill(start_color="ADD8E6", end_color="ADD8E6", fill_type="solid")  # 浅蓝色
    cell.font = Font(size=16)

# 设置行高和列宽
for row in range(1, sheet.max_row + 2):
    sheet.row_dimensions[row].height = 30

# 特别确保最后一行高度为30
sheet.row_dimensions[sheet.max_row].height = 30

# 表格宽度
for col in range(1, sheet.max_column + 1):
    column_letter = get_column_letter(col)
    sheet.column_dimensions[column_letter].width = 13.5

# 添加标题行
sheet.insert_rows(1)
sheet.merge_cells('A1:H1')
sheet.cell(row=1, column=1).value = title
sheet.cell(row=1, column=1).alignment = Alignment(horizontal='center', vertical='center')
sheet.cell(row=1, column=1).font = Font(size=18, bold=True)
sheet.cell(row=1, column=1).fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid") 

# 保存工作簿
excel_file = path + fr"\00-03 {title}.xlsx"
workbook.save(excel_file)
print(f"校历已生成: {excel_file}")

# ========== 转换为 PDF ==========
def excel_to_pdf(input_excel_path, output_pdf_path):
    try:
        # 启动 Excel 应用程序
        excel = win32com.client.Dispatch("Excel.Application")
        excel.Visible = False  # 不显示 Excel 界面
        
        # 打开 Excel 文件
        workbook = excel.Workbooks.Open(input_excel_path)
        
        # 设置打印区域(整个工作表)
        sheet = workbook.ActiveSheet
        sheet.PageSetup.Zoom = False
        sheet.PageSetup.FitToPagesWide = 1  # 适应宽度到 1 页
        sheet.PageSetup.FitToPagesTall = False  # 高度不限制
        
        # 导出为 PDF
        workbook.ExportAsFixedFormat(0, output_pdf_path)  # 0 表示 PDF 格式
        
        # 关闭 Excel
        workbook.Close(False)
        excel.Quit()
        
        print(f"PDF 已生成: {output_pdf_path}")
    except Exception as e:
        print(f"转换失败: {e}")

# 调用转换函数
pdf_file = path + fr"\00-03 {title}.pdf"
excel_to_pdf(excel_file, pdf_file)

time.sleep(2)

发到小红书上

相关推荐
Zzz 小生28 分钟前
LangChain Streaming-Overview:流式处理使用完全指南
人工智能·python·语言模型·langchain·github
yzx99101334 分钟前
Python数据结构入门指南:从基础到实践
开发语言·数据结构·python
百锦再1 小时前
Jenkins 全面精通指南:从入门到脚本大师
运维·后端·python·servlet·django·flask·jenkins
FYKJ_20101 小时前
springboot大学校园论坛管理系统--附源码42669
java·javascript·spring boot·python·spark·django·php
Loo国昌1 小时前
【AI应用开发实战】 03_LangGraph运行时与状态图编排:从直接执行到图编排的演进之路
人工智能·后端·python·自然语言处理·prompt
ValhallaCoder1 小时前
hot100-堆
数据结构·python·算法·
小小小米粒1 小时前
函数式接口 + Lambda = 方法逻辑的 “插拔式解耦”
开发语言·python·算法
Dr.Kun3 小时前
【鲲码园PsychoPy】延迟折扣任务(DDT)
python·psychopy·心理学编程
coding者在努力3 小时前
LangChain简介,最直白的介绍
人工智能·python·语言模型·langchain
癫狂的兔子4 小时前
【Python】【机器学习】支持向量积
python·机器学习