diff --git a/app/templates/日报模板.docx b/app/templates/日报模板.docx index cf976b1..ee74b25 100644 Binary files a/app/templates/日报模板.docx and b/app/templates/日报模板.docx differ diff --git a/app/tools/deal_excels.py b/app/tools/deal_excels.py index 646f7c9..c18044e 100644 --- a/app/tools/deal_excels.py +++ b/app/tools/deal_excels.py @@ -148,6 +148,49 @@ def deal_excel(start_time, end_time, file_path): except Exception as e: logger.exception(f"对数据按照’省‘进行分类汇总{e}") +def deal_excel_over_load(file_path): + try: + logger.info("开始分析配变过载excel") + + # 获取所有sheet页名称 + excel_file = pd.ExcelFile(file_path) + sheet_names = excel_file.sheet_names + + pattern_sheet = r"重过载明细" + + # 使用正则表达式进行模糊匹配(不区分大小写) + matched_sheets = [ + sheet + for sheet in sheet_names + if re.fullmatch(pattern_sheet, sheet, re.IGNORECASE) + ] + + if len(matched_sheets) == 1: + final_sheet = matched_sheets[0] + else: + logger.error("没有找到匹配的sheet页") + return None + + df = pd.read_excel( + file_path, + sheet_name=final_sheet, + ) + values_to_include = ['严重过载', '一般过载'] + filtered_df = df[df['重过载情况'].isin(values_to_include)] + grouped_df = filtered_df.groupby(['分子公司', '地市局']).size().reset_index(name='记录数') + sorted_df = grouped_df.sort_values(by='记录数', ascending=False) + top_5_results = sorted_df.head(5) + # ---------------------------------------去点中间或两侧空格--------------------------------- + top_5_results["分子公司"] = top_5_results["分子公司"].str.strip().str.replace(r"\s+", "", regex=True) + top_5_results["地市局"] = top_5_results["地市局"].str.strip().str.replace(r"\s+", "", regex=True) + # ---------------------------------------去点中间或两侧空格--------------------------------- + top_5_results['公司地市'] = top_5_results['分子公司'] + top_5_results['地市局'] + # 最终选择需要返回的列:'公司_地市' 和 '记录数' + final_output = top_5_results[['公司地市', '记录数']] + return final_output + except Exception as e: + logger.exception(f"对数据按照’省‘进行分类汇总{e}") + # 判断地市top5环比方法 def top5_dod_analysis(top, district_stat_before): diff --git a/app/tools/final_doc.py b/app/tools/final_doc.py index 5dcca1c..bfb86a9 100644 --- a/app/tools/final_doc.py +++ b/app/tools/final_doc.py @@ -10,10 +10,10 @@ from docx import Document from pathlib import Path from datetime import datetime from docx.shared import Inches, Pt -from app.tools.deal_excels import deal_excel, top5_dod_analysis, transform_data +from app.tools.deal_excels import deal_excel, top5_dod_analysis, transform_data, deal_excel_over_load 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, copy_table_no_align +from app.tools.replace_table import copy_table, copy_sta_table 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 @@ -61,8 +61,12 @@ def deal_docx(folder_path, save_path=None, time_type=0): file_path_dict["power_off_excel"] = folder_path + "/" + file continue + if file.endswith(".xlsx") and "停电报表配变明细" in file: + file_path_dict["over_load_excel"] = folder_path + "/" + file + continue + # 如果传入的文件不对,抛出异常 - if len(file_path_dict) != 3: + if len(file_path_dict) != 4: logger.exception("文件格式错误") raise HTTPException( status_code=400, @@ -113,9 +117,9 @@ def deal_docx(folder_path, save_path=None, time_type=0): continue # 第二点过载台数 - if re.search(r"过载(\d+)[条台]", para.text): + if re.search(r"过载(\d+)(?:[条台]|\s*)?", para.text): doc_dict["over_load"] = ( - re.search(r"过载(\d+)[条台]", para.text).group().replace("过载", "") + re.search(r"过载(\d+)(?:[条台]|\s*)?", para.text).group().replace("过载", "") ).replace("条", "").replace("台", "") # print(doc_dict['over_load']) continue @@ -365,10 +369,10 @@ def deal_docx(folder_path, save_path=None, time_type=0): standardize_date = ( f"{month_before}月{day_before}日17时至{month}月{day}日17时期间," ) - elif time_type == 1: # -------------------------20250429更新,修改开始和结束时间--------------------------------- - standardize_date = f"{year}年{month}月{day}日0时至24时期间," + #standardize_date = f"{year}年{month}月{day}日0时至24时期间," + standardize_date = f"{month}月{day}日0时至24时期间," # standardize_date = '' # -------------------------20250429更新,修改开始和结束时间--------------------------------- @@ -596,7 +600,8 @@ def deal_docx(folder_path, save_path=None, time_type=0): # 将数据组装相关的时间内容 doc_dict["first_point_para1"] = standardize_date + doc_dict["first_point_para1"] - doc_dict["sentiment_para"] = standardize_date + doc_dict["sentiment_para"] + #doc_dict["sentiment_para"] = standardize_date + doc_dict["sentiment_para"] + doc_dict["sentiment_para"] = doc_dict["sentiment_para"] # {{standardize_date}}全网收到{{complain_num}}条供电类投诉,环比{{complain_dod}}条; complain_text = ( @@ -766,6 +771,21 @@ def deal_docx(folder_path, save_path=None, time_type=0): is_dynamics=False ) + logger.info("将配变重过载的数据插入表格5") + # 统计配变过载前五的数据 + top_5_results = deal_excel_over_load(file_path_dict["over_load_excel"]) + # 表4中的插入位置 + start_tb4_row = 2 + start_tb4_col = 8 + if not top_5_results.empty: + data = top_5_results.values + copy_sta_table( + electricity_daily.tables[1], + data, + start_tb4_row, + start_tb4_col, + ) + # copy_sta_table(electricity_daily.tables[3], top5_list, start_tb4_row, start_tb4_col) # 将表格中的字体中文设置成仿宋,英文数字设置成新罗马,均为11号大小 diff --git a/app/tools/replace_table.py b/app/tools/replace_table.py index a84f311..b84466c 100644 --- a/app/tools/replace_table.py +++ b/app/tools/replace_table.py @@ -90,4 +90,4 @@ def copy_sta_table( target_table.cell(target_row, target_col).text = str(data[i][j]) except Exception as e: logger.exception(f"自行统计的数据插入word表格中失败: {e}") - raise e + raise e \ No newline at end of file diff --git a/upgrade.md b/upgrade.md index 1718112..b29072c 100644 --- a/upgrade.md +++ b/upgrade.md @@ -1,4 +1,12 @@ # 2025-07-11 1) 日报模板 2) tools/final_doc.py -3) tools/replace_table.py \ No newline at end of file +3) tools/replace_table.py +4) tools/find_before_word.py + +# 2025-07-15 +1) 日报模板 +2) tools/final_doc.py +3) tools/replace_table.py +4) tools/find_before_word.py +5) tools/deal_excels.py \ No newline at end of file