
背景需求:
20250214作为信息员,我会每周去区里的“信息平台”上报“教育教学”相关的通讯。因此领导在周五把每周的周计划安排表(docx)发我。

因为之前做了校历、营养员的人数统计表、各班点名册、本班周计划教案。
所以,我立刻从这份周安排里看到了“周次、起止月日、周一到周五、五天年月”

运用营养员的7天思路,把每周安排里的日期都批量做好吧
WORD模版
把第一周的模版分析需要哪些数据:
1、标题栏:周次、一周的周一(年月)、一周的周五(年月)

2、侧边栏:一周的每天的日期(月、日)、星期(周一到周五)

我觉得还是统一做一周7天的模版(如4月27日周日调休上班),同时双休日也有部分老师或领导也要干活(参加工会活动或者带孩子们参加OM比赛等)
所以模版统一做成7天,星期一到日的汉字直接写模版里,标题日期统一写周一到周日的日期(年月日)


对应的EXCLE标题制作
'''
把领导的每周安排的模版的日期批量做好(1)——制作日期数字
如果有空格,全部要加一个空格,否则读取出来的年月日是一位小数浮点数
星火讯飞,阿夏
2025年2月14日
'''
# -*- coding: utf-8 -*-
import datetime
import openpyxl
from openpyxl.styles import Alignment, PatternFill
import time
import pandas as pd
import re
from openpyxl import load_workbook
# for
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\20250214领导的每周安排模板'
# 1、制作基础日期表
title='2024学年第二学期校历'
tt=path+fr"\02-01 {title}.xlsx"
# 创建一个新的Excel工作簿
workbook = openpyxl.Workbook()
sheet = workbook.active
# 设置标题行
title_row = ["week", "D1", "D2", "D3", "D4", "D5", "D6", "D7"]
sheet.append(title_row)
# 设置日期范围
start_date = datetime.date(2025, 2, 17)
end_date = datetime.date(2025, 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列开始,所以加2year
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日、2025年1月1日,单元格填充为白色
special_dates = [datetime.date(2025, 4, 4), datetime.date(2025, 5, 1), datetime.date(2025, 5, 2), datetime.date(2025, 5, 5),datetime.date(2025, 6, 2)]
# for i in range(1, 8):
# special_dates.append(datetime.date(2024, 10, i))
# special_dates.append(datetime.date(2025, 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(2025, 4, 27) or current_day == datetime.date(2024, 9, 29) or current_day == datetime.date(2024, 10, 12):
if current_day == datetime.date(2025, 4, 27):
yellow_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
cell.fill = yellow_fill
# 移动到下一天
current_day += datetime.timedelta(days=1)
# 设置列宽
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')
# 打印所有黄色底纹填充格子的坐标
yellow_coordinates = []
for row in sheet.iter_rows():
for cell in row:
if cell.fill and cell.fill.start_color.rgb == 'FFFF00':
yellow_coordinates.append((cell.row, cell.column))
print("Yellow filled cells coordinates:", yellow_coordinates)
# 以下是拆分信息(提取第一天和最后一天)
#
# workbook.save(tt)
# time.sleep(1)
# # 读取第3列第二行开始的单元格文字,截取“年”前面的部分,写入第12列的第2行写入。同时在第12列的第1行写入“year”
# # 示例数据
# # b = [3, 4,5]
# # 加载已存在的Excel文件
# workbook = openpyxl.load_workbook(tt)
# sheet = workbook.active
n=1
# 读取标题中的信息
# 读取B列:
for i in range(2, sheet.max_row + 1):
cell_value = sheet.cell(row=i, column=2).value
# for x in range(9,12):
if cell_value:
match = re.search(r'(\d+)年', cell_value)
year_part = match.group(1)
sheet.cell(row=i, column=9).value = year_part
if cell_value:
match = re.search(r'年(\d+)月', cell_value)
month_part = match.group(1)
sheet.cell(row=i, column=10).value = month_part
if cell_value:
match = re.search(r'月(\d+)日', cell_value)
day_part = match.group(1)
sheet.cell(row=i, column=11).value = day_part
# 在第12列的第1行写入“year”
sheet.cell(row=1, column=9).value = f"year1"
sheet.cell(row=1, column=10).value = f"month1"
sheet.cell(row=1, column=11).value = f"day11"
# 读取H列:
for i in range(2,