diff --git a/app/templates/日报模板-2025-07-15.docx b/app/templates/日报模板-2025-07-15.docx new file mode 100644 index 0000000..8450fa0 Binary files /dev/null and b/app/templates/日报模板-2025-07-15.docx differ diff --git a/app/templates/日报模板.docx b/app/templates/日报模板.docx index 8450fa0..8983da6 100644 Binary files a/app/templates/日报模板.docx and b/app/templates/日报模板.docx differ diff --git a/app/tools/final_doc.py b/app/tools/final_doc.py index 6c0558b..7ad7489 100644 --- a/app/tools/final_doc.py +++ b/app/tools/final_doc.py @@ -13,7 +13,7 @@ from docx.shared import Inches, Pt from app.tools.deal_excels import deal_excel, top5_dod_analysis, transform_data from app.tools.get_time import get_time from app.tools.replace_text import replace_text_in_docx -from app.tools.replace_table import copy_table, copy_sta_table +from app.tools.replace_table import copy_table, copy_sta_table, copy_table_no_align from app.tools.style import table_style from app.tools.effective_date import get_next_day from app.tools.find_before_word import extract_overload_info_from_previous_day @@ -126,45 +126,23 @@ def deal_docx(folder_path, save_path=None, time_type=0): continue # 获取所有表格 - + print("获取投诉服务的表格") tables = doc_poweroff.tables # 舆情直接取第一个表格 + print("舆情的表格") table_sentiment = doc_sentiment.tables[0] - # 定义要查看的区域范围 - start_row1_1 = 2 # 起始行索引(从0开始) - end_row1_1 = 8 # 结束行索引(不包括) - start_col1_1 = 10 # 起始列索引(从0开始) - end_col1_1 = 13 # 结束列索引(不包括) - # 表1 “抢修、投诉、舆情”三工单监测汇总表 table1 = tables[0] - # 定义要查看的区域范围 - start_row1 = 2 # 起始行索引(从0开始) - end_row1 = 8 # 结束行索引(不包括) - start_col1 = 2 # 起始列索引(从0开始) - end_col1 = 9 # 结束列索引(不包括) # 表2配变过载监测汇总表 table2 = tables[1] - # 定义要查看的区域范围 - start_row2 = 1 # 起始行索引(从0开始) - end_row2 = 8 # 结束行索引(不包括) - start_col2 = 1 # 起始列索引(从0开始) - end_col2 = 4 # 结束列索引(不包括) - # 表3停电用户前五供电局 table3 = tables[2] - # 定义要查看的区域范围 - start_row3 = 2 # 起始行索引(从0开始) - end_row3 = 7 # 结束行索引(不包括) - start_col3 = 1 # 起始列索引(从0开始) - end_col3 = 5 # 结束列索引(不包括) - # 新增表4 95598供电类投诉前五供电局统计表 # table4 = doc_poweroff.add_table(6, 5) @@ -238,9 +216,6 @@ def deal_docx(folder_path, save_path=None, time_type=0): f"表1中剩余的省份统计数据及舆情的统计数据、环比情况:{table1_extra_data}" ) - # 省份统计的表格数据在表格中的起始位置 - start_row_pro_sta = 2 - start_col_pro_sta = 9 # 将昨天的地市统计转成字典 district_stat_before = dict(district_stat_before) @@ -256,6 +231,7 @@ def deal_docx(folder_path, save_path=None, time_type=0): top5_poweroff_list = [] # update:2025-07-04 修改供电类投诉前五供电局统计表的同排行 + """ need_district_statistics = ( district_statistics[0:5] if len(district_statistics) > 5 @@ -301,9 +277,8 @@ def deal_docx(folder_path, save_path=None, time_type=0): top5_name_list.append(f"其他{count + other_count}单位") top5_poweroff_list.append(poweroff_value) top_dod_dict["其他单位"] = "-" - - # old_version """ + # old_version if len(district_statistics) >= 5: # 地市前五统计 @@ -329,13 +304,11 @@ def deal_docx(folder_path, save_path=None, time_type=0): top_dod_dict[top5[0]] = top5_dod_analysis(top5, district_stat_before) elif 0 < len(district_statistics) < 5: - for i in range(len(district_statistics)): - top5_name_list.append(district_statistics[i][0]) top5_poweroff_list.append(district_statistics[i][1]) top_dod_dict[district_statistics[i][0]] = top5_dod_analysis(district_statistics[i], district_stat_before) - """ + print(f"地市前五名称{top5_name_list}") print(f"地市前五数据{top5_poweroff_list}") # top_dod_dict[top1[0]] = top5_dod_analysis(top1, district_stat_before) @@ -350,10 +323,6 @@ def deal_docx(folder_path, save_path=None, time_type=0): # 地市前5的名称、数据、环比放入列表并转至,方便写入表格4 top5_list = transform_data([top5_name_list, top5_poweroff_list, top5_stat_list]) - # 表4中的插入位置 - start_tb4_row = 2 - start_tb4_col = 1 - # 省总的投诉情况及环比 complain_dod = int(province_statistics_total) - int(province_stat_be_total) @@ -660,7 +629,12 @@ def deal_docx(folder_path, save_path=None, time_type=0): # 将表格添加到新的文档里 # 组装表1的数据 # 此处缺少省份统计数据和舆情数据 - logger.info("将数据写入word表格1") + logger.info("将投诉服务的表格1写入日报的表格1,从2-8行,2-9列写入") + # 定义要查看的区域范围 + start_row1 = 2 # 起始行索引(从0开始) + end_row1 = 8 # 结束行索引(不包括) + start_col1 = 2 # 起始列索引(从0开始) + end_col1 = 9 # 结束列索引(不包括) copy_table( table1, electricity_daily.tables[0], @@ -671,8 +645,12 @@ def deal_docx(folder_path, save_path=None, time_type=0): 0, ) + # 插入各个省份的投诉数据及环比 - logger.info("将自行统计的数据插入表格1") + logger.info("将投诉数据写入到表1,第二行开始,第11列开始") + # 省份统计的表格数据在表格中的起始位置 + start_row_pro_sta = 2 + start_col_pro_sta = 11 copy_sta_table( electricity_daily.tables[0], table1_extra_data, @@ -681,7 +659,12 @@ def deal_docx(folder_path, save_path=None, time_type=0): ) # 放入舆情的数据 - logger.info("将自行统计的数据插入表格2") + logger.info("将舆情数据写入到表1,第二行开始,第11列开始") + # 定义要查看的区域范围 + start_row1_1 = 2 # 起始行索引(从0开始) + end_row1_1 = 8 # 结束行索引(不包括) + start_col1_1 = 10 # 起始列索引(从0开始) + end_col1_1 = 13 # 结束列索引(不包括) copy_table( table_sentiment, electricity_daily.tables[0], @@ -689,11 +672,29 @@ def deal_docx(folder_path, save_path=None, time_type=0): end_row1_1, start_col1_1, end_col1_1, - 1, + 3, ) - # 复制表2的数据 - logger.info("将自行统计的数据插入表格3") + # 复制表1的数据 + logger.info("将配变过载写入表0,第二行开始,第9列开始") + # 定义要查看的区域范围 + start_row2 = 1 # 起始行索引(从0开始) + end_row2 = 7 # 结束行索引(不包括) + start_col2 = 2 # 起始列索引(从0开始) + end_col2 = 4 # 结束列索引(不包括) + target_row=1 + target_col=7 + copy_table_no_align( + table2, + electricity_daily.tables[0], + start_row2, + end_row2, + start_col2, + end_col2, + target_row, + target_col + ) + """ copy_table( table2, electricity_daily.tables[1], @@ -703,9 +704,25 @@ def deal_docx(folder_path, save_path=None, time_type=0): end_col2, 0, ) + """ # 复制表3的数据 - logger.info("将自行统计的数据插入表格4") + logger.info("将停电前五插入表格2") + # 定义要查看的区域范围 + start_row3 = 2 # 起始行索引(从0开始) + end_row3 = 7 # 结束行索引(不包括) + start_col3 = 1 # 起始列索引(从0开始) + end_col3 = 5 # 结束列索引(不包括) + copy_table( + table3, + electricity_daily.tables[1], + start_row3, + end_row3, + start_col3, + end_col3, + 0, + ) + """ copy_table( table3, electricity_daily.tables[2], @@ -715,18 +732,20 @@ def deal_docx(folder_path, save_path=None, time_type=0): end_col3, 0, ) - + """ # 填充表格4 # 需要判断是否前五数据不存在 logger.info("将自行统计的数据插入表格5") + # 表4中的插入位置 + start_tb4_row = 2 + start_tb4_col = 5 if top5_list: copy_sta_table( - electricity_daily.tables[3], + electricity_daily.tables[1], top5_list, start_tb4_row, start_tb4_col, - is_dynamics=True, - length=5, + is_dynamics=False ) # copy_sta_table(electricity_daily.tables[3], top5_list, start_tb4_row, start_tb4_col) diff --git a/app/tools/replace_table.py b/app/tools/replace_table.py index d5eb242..a84f311 100644 --- a/app/tools/replace_table.py +++ b/app/tools/replace_table.py @@ -21,11 +21,30 @@ def copy_table( # 获取源表格单元格的内容 source_cell = source_table.cell(i, j) # 将内容复制到目标表格的对应单元格 + target_table.cell(i, j+flag).text = source_cell.text + """ if flag == 0: target_table.cell(i, j).text = source_cell.text if flag == 1: # j+1即表给从后一列开始添加数据 target_table.cell(i, j + 1).text = source_cell.text + """ + except Exception as e: + logger.exception(f"复制表格数据时发生错误: {e}") + raise e + +def copy_table_no_align( + source_table, target_table, start_row, end_row, start_col, end_col, target_row,target_col +): + try: + logger.info("遍历源表格的指定范围,将数据复制到目标表格的相同位置") + # 遍历源表格的指定范围,将数据复制到目标表格的相同位置 + for i in range(start_row, end_row): + for j in range(start_col, end_col): + # 获取源表格单元格的内容 + source_cell = source_table.cell(i, j) + # 将内容复制到目标表格的对应单元格 + target_table.cell(target_row+i, target_col+j).text = source_cell.text except Exception as e: logger.exception(f"复制表格数据时发生错误: {e}") raise e @@ -39,7 +58,6 @@ def copy_sta_table( start_row, start_col, is_dynamics: bool = None, - length: int = None, ): try: logger.info(f"开始将自行统计的数据插入word表格中,{target_table}") diff --git a/upgrade.md b/upgrade.md new file mode 100644 index 0000000..1718112 --- /dev/null +++ b/upgrade.md @@ -0,0 +1,4 @@ +# 2025-07-11 +1) 日报模板 +2) tools/final_doc.py +3) tools/replace_table.py \ No newline at end of file