【办公类-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)

发到小红书上

相关推荐
2301_765703143 小时前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
Stuomasi_xiaoxin3 小时前
Windows 安装 OpenCode 完全指南:从 npm 踩坑到 Chocolatey 完美部署
python·ai编程·opencode
2301_790300963 小时前
深入理解Python的if __name__ == ‘__main__‘
jvm·数据库·python
我的xiaodoujiao3 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 45--生成项目需要的requirements.txt依赖文件
python·学习·测试工具·pytest
helloworldandy3 小时前
趣味项目与综合实战
jvm·数据库·python
ctyshr3 小时前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
WangYaolove13143 小时前
基于opencv的疲劳检测系(源码+文档)
python·mysql·django·毕业设计·源码
90的程序爱好者4 小时前
flask入门
后端·python·flask
历程里程碑4 小时前
滑动窗口----滑动窗口最大值
javascript·数据结构·python·算法·排序算法·哈希算法·散列表