☰
【办公类-53-13】20261019(预测)Python模仿制作2026学年第二学期校历的日期提取
2026/10/21 2:37:31 网站建设 项目流程

一、背景需求

【办公类-53-12】20261016(预测)Python模仿制作2026学年第二学期校历(excel+pdf,上下学期都包含)https://mp.csdn.net/mp_blog/creation/editor/168142503

然后要制作周次、日期、长短日期、星期等

二、制作日期基础信息

把四个年级资料周计划内四类活动的代码复制

又增加了一个py

(一)0401 提取黄色的日期和周次(黄色有星期教案用)

''' 2026学年第二学期校历,制作教案用日期列表,人工修改 星火讯飞 阿夏 20270202 ''' # -*- coding: utf-8 -*- import datetime import openpyxl from openpyxl.styles import Alignment, PatternFill import time,os # for path=r'D:\Python最终内容\20270202四个年龄各类表格班本化' os.makedirs(path,exist_ok=True) title='2026学年第二学期校历' tt=path+fr"\02-01 {title}.xlsx" # 创建一个新的Excel工作簿 workbook = openpyxl.Workbook() sheet = workbook.active # 设置标题行 title_row = ["周次", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"] sheet.append(title_row) # 设置日期范围 start_date = datetime.date(2027, 2, 22) end_date = datetime.date(2027,6, 30) # 计算周数和日期 current_week = 1 current_day = start_date while current_day <= end_date: # 获取当前周的第一天(星期一) week_start = current_day - datetime.timedelta(days=current_day.weekday()) # 如果当前日期是新的一周,添加新行并更新周数 if current_day == week_start: sheet.append([f"第{current_week:02d}周"]) current_week += 1 # 在正确的单元格中添加日期 column_index = current_day.weekday() + 2 # 从B列开始,所以加2 cell = sheet.cell(row=current_week, column=column_index) # 不同的表示方法 # cell.value = current_day.strftime("%Y-%m-%d") # 2024-09-01 # cell.value = current_day.strftime("%Y-%#m-%#d") # 2024-9-1 # cell.value = current_day.strftime("%m/%d") # 09-01 # cell.value = current_day.strftime("%#m/%#d") # 9-1 # cell.value = current_day.strftime('%Y{y}%m{m}%d{d}').format(y='年', m='月', d='日') # 2024年09月01日 cell.value = current_day.strftime('%Y{y}%#m{m}%#d{d}').format(y='年', m='月', d='日') # 2024年9月1日 cell.alignment = Alignment(horizontal='center', vertical='center') # 如果日期不等于周六和周日,就把这个单元格填充为浅黄色 if current_day.weekday() not in (5, 6): light_yellow_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid") cell.fill = light_yellow_fill # 如果日期等于2024-09-14、2024-10-12、9月16日、9月17日、10月1日到10月8日、2027年1月1日,单元格填充为白色 special_dates = [datetime.date(2027, 4, 5), # datetime.date(2027, 9, 25), datetime.date(2027, 5, 3), datetime.date(2027, 5, 4),datetime.date(2027, 5, 5), datetime.date(2027, 6, 9),] # for i in range(1, 8): # special_dates.append(datetime.date(2024, 10, i)) # special_dates.append(datetime.date(2027, 1, 1)) if current_day in special_dates: white_fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid") cell.fill = white_fill # # 如果日期等于2024-09-14或2024-10-12,单元格填充为黄色 # if current_day == datetime.date(2027, 4, 27) or current_day == datetime.date(2024, 9, 29) or current_day == datetime.date(2024, 10, 12): special_yellow_dates = [datetime.date(2027, 4, 25), datetime.date(2027, 5, 8), ] if current_day in special_yellow_dates: yellow_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid") cell.fill = yellow_fill # 移动到下一天 current_day += datetime.timedelta(days=1) # 保存工作簿 workbook.save(tt) time.sleep(2) import openpyxl from openpyxl.styles import PatternFill # 打开已存在的Excel文件 workbook = openpyxl.load_workbook(tt) sheet = workbook.active # 设置黄色填充样式 yellow_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid") # 提取单独的第一个黄、最后一个黄 # 遍历每一行 for row in range(2, sheet.max_row + 1): # 读取每行里面的所有黄色单元格,复制到I列开始的单元格 yellow_cells = [] for col in range(2, sheet.max_column + 1): cell = sheet.cell(row=row, column=col) if cell.fill == yellow_fill: yellow_cells.append(cell.value) for index, value in enumerate(yellow_cells): sheet.cell(row=row, column=10+index).value = value # 定义黄色填充样式 yellow_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid") for row in range(2, sheet.max_row + 1): # 读取每行里面的所有黄色单元格,复制到I列开始的单元格 yellow_cells = [] for col in range(2, sheet.max_column + 1): cell = sheet.cell(row=row, column=col) if cell.fill == yellow_fill: yellow_cells.append(cell.value) # 获取黄色单元格所在列的第一行的文字 first_row_value = sheet.cell(row=1, column=col).value # 将文字写入R列对应的单元格 sheet.cell(row=row, column=16+len(yellow_cells)).value = first_row_value # 将黄色单元格的值复制到I列开始的单元格 # for index, value in enumerate(yellow_cells): # sheet.cell(row=row, column=9+index).value = value # 设置列宽 for col in range(1, 30): sheet.column_dimensions[openpyxl.utils.get_column_letter(col)].width = 15 # for col in range(9, 12): # sheet.column_dimensions[openpyxl.utils.get_column_letter(col)].width = 35 # 设置单元格文字居中 for row in sheet.iter_rows(): for cell in row: cell.alignment = openpyxl.styles.Alignment(horizontal='center', vertical='center') workbook.save(tt) # 再以下这份里面手动复制黏贴起止日期 workbook.save(tt[:-5]+'人工修改用.xlsx')

我决定第9周6天、第10周5天,第11周3天(5天)

(二)人工修改

三、制作长短日期

''' 2026学年第二学期校历,制作教案用日期 提取长日期和短日期手动修改(日期必须是常规文本格式) 做教案用 星火讯飞 阿夏 20270903 ''' import openpyxl path=r'D:\Python最终内容\20270202四个年龄各类表格班本化' title='2026学年第二学期校历' # 打开Excel文件 file_path = path+fr"\02-01 {title}人工修改用.xlsx" workbook = openpyxl.load_workbook(file_path) sheet = workbook.active # 删除B列到I列 反复删除第2列 for x in range(8): for col in range(2,3): # B列是第2列,I列是第9列,所以范围是2到9 sheet.delete_cols(col) # 在A列后面插入两个空列 sheet.insert_cols(2, 2) import openpyxl # 遍历每一行,读取D列和H列的值,组合后写入B列 for row in range(2, sheet.max_row + 1): d_value = sheet.cell(row=row, column=4).value # D列的值 h_value = sheet.cell(row=row, column=8).value # H列的值 if h_value is not None: # 检查h_value是否为None combined_value = f"{d_value}——{h_value[5:]}" # 组合值,只保留H列单元格内容的[5:]部分 sheet.cell(row=row, column=2).value = combined_value # 写入B列 else: print(f"Row {row} has a None value in column H") for row in range(2, sheet.max_row + 1): d_value = sheet.cell(row=row, column=4).value # D列的值 h_value = sheet.cell(row=row, column=8).value # H列的值 combined_value = f"{d_value}—{h_value}" # 组合值 sheet.cell(row=row, column=3).value = combined_value # 写入B列 for col in range(2, 4): sheet.column_dimensions[openpyxl.utils.get_column_letter(col)].width = 30 # 删除IJ列 for x in range(2): for col in range(9,10): # B列是第2列,I列是第9列,所以范围是2到9 sheet.delete_cols(col) # 保存修改后的Excel文件 workbook.save(path+fr"\02 {title}组合教案用.xlsx")

(三)手动修改“组合教案”

(四)制作20个班级的周计划模版(这里用不到)

三、制作长短日期、星期成功

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询