From 0535bda51a785072c913d6da4a316d6fde73a54d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=A0=87?= Date: Tue, 29 Jul 2025 14:55:09 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=F0=9F=90=9B:add=207-29?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/tools/count_data.py | 10 ++++++++-- app/tools/deal_excels.py | 40 ++++++++++++++++++++++++++++++++++------ app/tools/final_doc.py | 19 ++++++++++++------- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/app/tools/count_data.py b/app/tools/count_data.py index 26a0193..706751b 100644 --- a/app/tools/count_data.py +++ b/app/tools/count_data.py @@ -1,5 +1,8 @@ import re +from numpy.ma.core import floor + + # text = "6月15日17时至6月16日17时期间,全网累计停电132.59万户次(5分钟以内短时停电用户23.48万户次),环比减少68.28万户次,其中,重要用户停电0户次,用户停电情况总体平稳。" @@ -12,14 +15,16 @@ def count_change_outage(text): total_outage = float(matches[0]) # 累计停电用户数 short_term_outage = float(matches[1]) # 短时停电用户数 change_outage = float(matches[2]) # 环比变化用户数 - + total_yesterday =0 # 判断是增加还是减少 if "减少" in text: result = change_outage / (total_outage + change_outage) type = "减少" + total_yesterday = total_outage + change_outage elif "增加" in text: result = change_outage / (total_outage - change_outage) type = "增加" + total_yesterday = total_outage - change_outage else: result = None # 或者其他默认值 @@ -42,7 +47,7 @@ def count_change_outage(text): else: important_stop_outage = "0" print("未找到重要用户停电户次") - + total_yesterday_formatted = round(total_yesterday, 2) return ( total_outage, short_term_outage, @@ -51,6 +56,7 @@ def count_change_outage(text): short_percentage, important_stop_outage, type, + total_yesterday_formatted ) diff --git a/app/tools/deal_excels.py b/app/tools/deal_excels.py index 0e3e3d3..5b2a3d4 100644 --- a/app/tools/deal_excels.py +++ b/app/tools/deal_excels.py @@ -179,16 +179,44 @@ def deal_excel_over_load(file_path): 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 = 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) + sorted_df["分子公司"] = sorted_df["分子公司"].str.strip().str.replace(r"\s+", "", regex=True) + sorted_df["地市局"] = sorted_df["地市局"].str.strip().str.replace(r"\s+", "", regex=True) # ---------------------------------------去点中间或两侧空格--------------------------------- - top_5_results['公司地市'] = top_5_results['分子公司'] + top_5_results['地市局'] + sorted_df['公司地市'] = sorted_df['分子公司'] + sorted_df['地市局'] #top_5_results.loc[:, '公司地市'] = top_5_results['分子公司'] + top_5_results['地市局'] # 最终选择需要返回的列:'公司_地市' 和 '记录数' - final_output = top_5_results[['公司地市', '记录数']] - return final_output + final_output = sorted_df[['公司地市', '记录数']].values.tolist() + overload_top5_list = [] + if final_output : + need_final_output = ( + final_output[0:5] + if len(final_output) > 5 + else final_output + ) + other_final_output = ( + final_output[5:] if len(final_output) > 5 else [] + ) + other_count = 0 + if ( + len(other_final_output) > 0 + and final_output[4][1] == final_output[5][1] + ): + for i in range(len(other_final_output)): + if other_final_output[i][1] == final_output[4][1]: + other_count += 1 + overload_value = need_final_output[len(need_final_output) - 1][1] + count = 0 + for i in range(len(need_final_output)): + current_final_output = need_final_output[i][1] + if current_final_output == overload_value and other_count >0: + count += 1 + else: + overload_top5_list.append(need_final_output[i]) + if count >0 : + overload_top5_list.append([f"其他{count + other_count}供电局",overload_value]) + return overload_top5_list except Exception as e: logger.exception(f"对数据按照’省‘进行分类汇总{e}") diff --git a/app/tools/final_doc.py b/app/tools/final_doc.py index bec5dbe..70da980 100644 --- a/app/tools/final_doc.py +++ b/app/tools/final_doc.py @@ -272,7 +272,7 @@ def deal_docx(folder_path, save_path=None, time_type=0,over_load_value=0): count = 0 for i in range(len(need_district_statistics)): current_poweroff_value = need_district_statistics[i][1] - if current_poweroff_value == poweroff_value: + if current_poweroff_value == poweroff_value and other_count >0: count += 1 else: top5_name_list.append(need_district_statistics[i][0]) @@ -280,6 +280,11 @@ def deal_docx(folder_path, save_path=None, time_type=0,over_load_value=0): top_dod_dict[need_district_statistics[i][0]] = top5_dod_analysis( need_district_statistics[i], district_stat_before ) + if count >0 : + top5_name_list.append(f"其他{count + other_count}家单位") + top5_poweroff_list.append(poweroff_value) + top_dod_dict["其他单位"] = "—" + """ if count == 1 and other_count==0: top5_name_list.append( need_district_statistics[len(need_district_statistics) - 1][0] @@ -297,7 +302,7 @@ def deal_docx(folder_path, save_path=None, time_type=0,over_load_value=0): top5_name_list.append(f"其他{count + other_count}家单位") top5_poweroff_list.append(poweroff_value) top_dod_dict["其他单位"] = "—" - + """ # old_version """ if len(district_statistics) >= 5: @@ -429,6 +434,7 @@ def deal_docx(folder_path, save_path=None, time_type=0,over_load_value=0): short_precentage, important_stop_outage, type, + total_yesterday ) = count_change_outage(doc_dict["have_important"]) # 获取舆情数字信息 today_sentiment, type_sentiment, yesterday_sentiment, result_sentiment = ( @@ -515,10 +521,9 @@ def deal_docx(folder_path, save_path=None, time_type=0,over_load_value=0): # 替换模板字符串 replace_text_in_docx(electricity_daily_simple, replacements_simple) - datas = { "停电用户\n(万户)": { - "昨天": total_outage + change_outage, + "昨天": total_yesterday, "今天": total_outage, }, "过载配变\n(台)": {"昨天": over_load_before, "今天": doc_dict_over_load}, @@ -788,11 +793,11 @@ def deal_docx(folder_path, save_path=None, time_type=0,over_load_value=0): # 表4中的插入位置 start_tb4_row = 2 start_tb4_col = 8 - if not top_5_results.empty: - data = top_5_results.values + if top_5_results: + #data = top_5_results.values copy_sta_table( electricity_daily.tables[1], - data, + top_5_results, start_tb4_row, start_tb4_col, )