【办公类-54-11】20260901营养员《每周人数统计表》(双休日和国定假日填充灰色或填充白色、EXCEL+PDF,三个园区两种代码和两个excel,有XX节放假)

【办公类-54-11】20260901营养员《每周人数统计表》(双休日和国定假日填充灰色或填充白色、EXCEL+PDF,三个园区两种代码和两个excel,有XX节放假)
【办公类-54-10】20260201营养员《每周人数统计表》双休日和国定假日填充灰色或填充白色、EXCELPDF三个园区两种代码和两个excel有XX节放假https://mp.csdn.net/mp_blog/creation/editor/157586924背景需求复制一份做营养员的单子因为目前分园不需要做记录单只有4个班级可以写在小黑板上所以只要做总园10-12个班级小黑板写不下。基础信息修改应该是9月1日周二-1月22日周五但是为了凑满一周7天所以前后日期都补从8月31日周一-1月24日周日01-01先做日期Excel表格先复制一个模版.xlsx 2025学年第一学期校历,制作营养员的表格只要7天正好从周一开始 1\先批量excle表格获取日期 星火讯飞 阿夏 20260201 # -*- coding: utf-8 -*- import datetime import openpyxl from openpyxl.styles import Alignment, PatternFill import time,os # for pathrC:\Users\jg2yXRZ\OneDrive\桌面\20260702营养员表格 os.makedirs(path,exist_okTrue) title2026学年第一学期校历 # 创建一个新的Excel工作簿 workbook openpyxl.Workbook() sheet workbook.active # 设置标题行 title_row [周次, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期日] sheet.append(title_row) # 设置日期范围 start_date datetime.date(2026, 8,31) # 应该是6月30日但是那天是周二我需要凑满一周七天 end_date datetime.date(2027, 1, 24) # 计算周数和日期 current_week 1 current_day start_date while current_day end_date: # 获取当前周的第一天星期一 week_start current_day - datetime.timedelta(dayscurrent_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(rowcurrent_week, columncolumn_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(horizontalcenter, verticalcenter) # 移动到下一天 current_day datetime.timedelta(days1) # 在A列后面插入两个空列 # 前面B2,后面1代表插入几列 sheet.insert_cols(2, 1) # import openpyxl # 遍历每一行读取D列和H列的值组合后写入B列 # for row in range(2, sheet.max_row 1): # d_value sheet.cell(rowrow, column4).value # D列的值 # h_value sheet.cell(rowrow, column10).value # H列的值 # if h_value is not None: # 检查h_value是否为None # combined_value f{d_value}——{h_value} # 组合值只保留H列单元格内容的[5:]部分 # sheet.cell(rowrow, column2).value combined_value # 写入B列 # else: # print(fRow {row} has a None value in column H) # B列插入合并日期 for row in range(2, sheet.max_row 1): d_value sheet.cell(rowrow, column3).value # C列周一 h_value sheet.cell(rowrow, column9).value # I列周日 # 处理空值 if d_value is None: d_value 无日期 if h_value is None: h_value 2026年1月30日 # 组合值 combined_value f{d_value}—{h_value} print(fRow {row}: {combined_value}) # 写入B列 sheet.cell(rowrow, column2).value combined_value # for col in range(2, 4): # sheet.column_dimensions[openpyxl.utils.get_column_letter(col)].width 30 # 保存工作簿 workbook.save(pathfr\01_{title}长日期第XX周.xlsx) time.sleep(2)二、填充灰色或白色 营养员的每周人数统计表18/22周通用灰色填充节日标注PDF导出 双休/国定假日灰色填充灰色行标注清明节放假劳动节放假等字样 班级和门牌号列表写入自动读取校历周次日期生成ExcelPDF 星火讯飞阿夏 2026年3月2日 import pandas as pd import openpyxl from openpyxl import load_workbook import copy from openpyxl.styles import PatternFill, Font, Alignment from openpyxl.worksheet.header_footer import HeaderFooter import itertools import os import win32com.client as win32 # 需安装pywin32: pip install pywin32 # ---------------------- 1. 基础配置信息可直接修改---------------------- pathrC:\Users\jg2yXRZ\OneDrive\桌面\20260702营养员表格 # 工作目录 titles 2026学年第一学期校历 # 学期名称 days 20260901 # 操作日期 school 上海市XXXX幼儿园 # 幼儿园名称 names 20260901总园每周人数统计 # 文件前缀模版名字不能变 template_name 00_模板总园每周人数统计.xlsx # 模板文件名 calendar_name f01_{titles}长日期第XX周.xlsx # 校历文件名 # 班级配置列表按模板结构第3-4行上午13-14行下午 class_config { morning: [ [班级, 304, 303, 203, 202, 201, 101, ], # 第3行 [星期, 中一, 中二, 中三, 中四, 中五, 中六, ] # 第4行 ], afternoon: [ [班级, 103, 104, 105, 204, 205, , ], # 第13行 [星期, 大三, 大二, 大一, 大四, 大五, , ] # 第14行 ] } # 上学期 # 节日放假标注配置周数, 星期(1-7), 标注文字1周一7周日 holiday_markers [ (4, 5, 中秋节放假), (4, 6, 中秋节放假), (4, 7, 中秋节放假), (5, 4, 国庆节放假), (5, 5, 国庆节放假), (5, 6, 国庆节放假), (5, 7, 国庆节放假), (6, 1, 国庆节放假),(6, 2, 国庆节放假), (6, 3, 国庆节放假), (18, 5, 元旦放假),(18, 6, 元旦放假),(18, 7, 元旦放假), ] # 特殊周休息日配置周数休息日数字串如6:167第6周1/6/7日休息 special_holiday_weeks {1: 167, 3: 6, 4: 567, 5: 4567, 6: 1237,18:567} # 调休上班配置周数变白日期数字串如10:6第10周6日调休上班 adjust_workdays {3: 7,6: 6} # 下学期 # # 特殊周休息日配置周数休息日数字串如6:167第6周1/6/7日休息 # special_holiday_weeks {6: 167, 9: 567, 10: 127, 16: 567, 18: 34567} # # 调休上班配置周数变白日期数字串如10:6第10周6日调休上班 # adjust_workdays {10: 6} # # 节日放假标注配置周数, 星期(1-7), 标注文字1周一7周日 # holiday_markers [ # (5, 6, 清明节放假), (5, 7, 清明节放假), (6, 1, 清明节放假), # (9, 5, 劳动节放假), (9, 6, 劳动节放假), (9, 7, 劳动节放假), # (10, 1, 劳动节放假), (10, 2, 劳动节放假), # (16, 5, 端午节放假), (16, 6, 端午节放假), (16, 7, 端午节放假), # (18, 3, 暑假放假) # ] # # 特殊周休息日配置周数休息日数字串如6:167第6周1/6/7日休息 # special_holiday_weeks {6: 167, 9: 567, 10: 127, 16: 567, 18: 34567} # # 调休上班配置周数变白日期数字串如10:6第10周6日调休上班 # adjust_workdays {10: 6} # 样式定义统一管理方便修改 light_gray_fill PatternFill(start_colorD3D3D3, end_colorD3D3D3, fill_typesolid) white_fill PatternFill(start_colorFFFFFF, end_colorFFFFFF, fill_typesolid) holiday_font Font(name微软雅黑, size12, boldFalse, color000000) # 标注字体 center_align Alignment(horizontalcenter, verticalcenter) # 居中对齐 weekday_rows [4,5,6,7,8,9,10] # 周一至周日对应上午行号下午10即可 # ---------------------- 2. 读取校历周次和日期 ---------------------- print( 1、读取校历周次和日期 ) calendar_path os.path.join(path, calendar_name) if not os.path.exists(calendar_path): raise FileNotFoundError(f校历文件不存在{calendar_path}) df pd.read_excel(calendar_path) col1 df.iloc[:, 0].tolist() # 周次列表[第01周, 第02周, ...] col2 df.iloc[:, 1].tolist() # 日期列表[2026X月X日-X月X日, ...] print(f成功读取 {len(col1)} 周数据) print(f周次{col1[:3]}...) print(f日期{col2[:3]}...) # ---------------------- 3. 复制模板创建多周工作表 ---------------------- print(\n 2、复制模板创建各周工作表 ) template_path os.path.join(path, template_name) if not os.path.exists(template_path): raise FileNotFoundError(f模板文件不存在{template_path}) wb load_workbook(template_path) ws_template wb.active # 遍历创建每周工作表 for i in range(len(col1)): week_num int(col1[i][1:3]) date_range col2[i] sheet_title f总园 每周人数统计 第 {week_num} 周 ({date_range}) new_sheet_name col1[i] # 创建新工作表并复制模板格式/内容 new_ws wb.create_sheet(titlenew_sheet_name) for row in ws_template.iter_rows(): for cell in row: # 复制单元格值和样式 new_cell new_ws[cell.coordinate] new_cell.value cell.value if cell.has_style: new_cell.font copy.copy(cell.font) new_cell.border copy.copy(cell.border) new_cell.fill copy.copy(cell.fill) new_cell.number_format copy.copy(cell.number_format) new_cell.alignment copy.copy(cell.alignment) # 复制行高列宽 new_ws.row_dimensions[cell.row].height ws_template.row_dimensions[cell.row].height col_letter openpyxl.utils.get_column_letter(cell.column) new_ws.column_dimensions[col_letter].width ws_template.column_dimensions[col_letter].width # 复制页边距 new_ws.page_margins copy.copy(ws_template.page_margins) # 设置标题并合并A1:H1 new_ws[A1] sheet_title new_ws.merge_cells(A1:H1) # 填充班级信息 print(f 填充{new_sheet_name}班级信息) # 上午3-4行循环起始行2/3 for row_idx, row_data in enumerate(class_config[morning], start2): for col_idx, value in enumerate(row_data, start1): new_ws.cell(rowrow_idx, columncol_idx, valuevalue) # 下午13-14行循环起始行12/13 for row_idx, row_data in enumerate(class_config[afternoon], start12): for col_idx, value in enumerate(row_data, start1): new_ws.cell(rowrow_idx, columncol_idx, valuevalue) # 删除原始模板工作表保存初始Excel del wb[wb.sheetnames[0]] excel_output_path os.path.join(path, f{days}_{names[8:]}_{titles}.xlsx) wb.save(excel_output_path) print(f 初始Excel保存至{excel_output_path}) # ---------------------- 4. 灰色填充节日放假标注 ---------------------- print(\n 3、灰色填充休息日标注节日放假 ) workbook load_workbook(excel_output_path) sheet_names workbook.sheetnames for sheet_idx, sheet_name in enumerate(sheet_names): current_week sheet_idx 1 ws workbook[sheet_name] print(f 处理{sheet_name}第{current_week}周) # 步骤1默认周末6/7日灰色填充 for idx in [5,6]: # 周六5周日6weekday_rows索引 morning_row weekday_rows[idx] afternoon_row morning_row 10 for row in [morning_row, afternoon_row]: for col in range(1, 9): ws.cell(rowrow, columncol).fill light_gray_fill # 步骤2标注节日放假文字B列 for week_num, weekday, holiday_name in holiday_markers: if current_week week_num: q1 weekday_rows[weekday-1] # 上午行 q2 q1 10 # 下午行 # 上午标注 cell_am ws.cell(rowq1, column2) cell_am.value holiday_name cell_am.font holiday_font cell_am.alignment center_align # 下午标注 cell_pm ws.cell(rowq2, column2) cell_pm.value holiday_name cell_pm.font holiday_font cell_pm.alignment center_align print(f 标注{holiday_name}) # 步骤3特殊周休息日灰色填充保留标注 if current_week in special_holiday_weeks: rest_days special_holiday_weeks[current_week] for day_str in rest_days: p int(day_str) q1 weekday_rows[p-1] q2 q1 10 for row in [q1, q2]: for col in range(1, 9): cell ws.cell(rowrow, columncol) current_val cell.value cell.fill light_gray_fill # 恢复B列标注文字 if col 2 and current_val is not None: cell.value current_val cell.font holiday_font cell.alignment center_align # 步骤4调休上班白色填充 if current_week in adjust_workdays: work_days adjust_workdays[current_week] for day_str in work_days: p int(day_str) q1 weekday_rows[p-1] q2 q1 10 for row in [q1, q2]: for col in range(1, 9): ws.cell(rowrow, columncol).fill white_fill print(f 调休上班星期{work_days}变白) # 保存灰色填充标注后的Excel workbook.save(excel_output_path) print( 灰色填充节日标注完成) # ---------------------- 5. 设置页眉 ---------------------- print(\n 4、设置工作表页眉 ) workbook load_workbook(excel_output_path) for sheet_name in workbook.sheetnames: ws workbook[sheet_name] # 设置页眉文字简化版和第二段代码保持一致 ws.oddHeader.center.text school ws.oddHeader.center.size 12 ws.oddHeader.center.font Arial workbook.save(excel_output_path) print(f 页眉设置完成已保存至Excel) # ---------------------- 6. Excel转PDF修复边距问题---------------------- print(\n 5、Excel转换为PDF ) def excel_to_pdf(excel_path, pdf_path, school_name): Excel转PDF修复边距显示问题 :param excel_path: Excel文件路径 :param pdf_path: PDF输出路径 :param school_name: 页眉显示的幼儿园名称 try: # 初始化Excel应用 excel win32.Dispatch(Excel.Application) excel.Visible False # 不显示Excel界面 excel.DisplayAlerts False # 屏蔽弹窗 # 打开Excel文件关键使用原生Excel打开保留所有格式 wb excel.Workbooks.Open(excel_path) # 遍历所有工作表重新设置页眉确保生效 for sheet in wb.Worksheets: # 重新设置页眉覆盖openpyxl的设置确保PDF中显示正确 sheet.PageSetup.CenterHeader school_name # 关键修复使用Excel原生的边距设置而不是openpyxl的 # 如果你需要自定义边距可以取消下面的注释并调整数值单位英寸 # sheet.PageSetup.LeftMargin 0.98 # 左边距 # sheet.PageSetup.RightMargin 0.98 # 右边距 # sheet.PageSetup.TopMargin 1.0 # 上边距 # sheet.PageSetup.BottomMargin 1.0 # 下边距 # 导出PDF核心修复使用Excel原生导出保留正确的边距 wb.ExportAsFixedFormat( Type0, # 0PDF1XPS Filenamepdf_path, Quality0, # 0标准1最小 IncludeDocPropertiesTrue, IgnorePrintAreasFalse, OpenAfterPublishFalse ) # 关闭文件 wb.Close(False) excel.Quit() return True except Exception as e: print(f 转换失败{str(e)}) try: excel.Quit() except: pass return False # 定义PDF保存路径 pdf_output_path os.path.join(path, f{days}_{names[8:]}_{titles}.pdf) # 执行转换使用修复后的函数 if excel_to_pdf(excel_output_path, pdf_output_path, school): print(f PDF保存至{pdf_output_path}) else: print( PDF转换失败请检查pywin32安装或Excel文件是否打开) # ---------------------- 7. 操作完成验证 ---------------------- print(\n 6、操作完成验证 ) excel_exist os.path.exists(excel_output_path) pdf_exist os.path.exists(pdf_output_path) if pdf_output_path else False if excel_exist: print(f✅ Excel文件创建成功{excel_output_path}) else: print(❌ Excel文件创建失败) if pdf_exist: print(f✅ PDF文件创建成功{pdf_output_path}) else: print(⚠️ PDF文件未创建若需PDF请检查依赖和文件状态) # 验证班级信息示例 if excel_exist: verify_wb load_workbook(excel_output_path) sample_sheet verify_wb[verify_wb.sheetnames[0]] print(f\n 示例验证{verify_wb.sheetnames[0]}) print(f 上午第3行{[sample_sheet.cell(3, col).value for col in range(1,8)]}) print(f 上午第4行{[sample_sheet.cell(4, col).value for col in range(1,8)]}) print(f 下午第13行{[sample_sheet.cell(13, col).value for col in range(1,8)]}) print(f 下午第14行{[sample_sheet.cell(14, col).value for col in range(1,8)]}) print(\n 全部操作完成 )PDF