Compare commits

...

2 Commits

Author SHA1 Message Date
许标 44d3f72ff3 feat 🐛:add templetemt 2 days ago
许标 de1839b27d feat 🐛:add before 3 days ago

Binary file not shown.

@ -148,6 +148,49 @@ def deal_excel(start_time, end_time, file_path):
except Exception as e: except Exception as e:
logger.exception(f"对数据按照’省‘进行分类汇总{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环比方法 # 判断地市top5环比方法
def top5_dod_analysis(top, district_stat_before): def top5_dod_analysis(top, district_stat_before):

@ -10,10 +10,10 @@ from docx import Document
from pathlib import Path from pathlib import Path
from datetime import datetime from datetime import datetime
from docx.shared import Inches, Pt 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.get_time import get_time
from app.tools.replace_text import replace_text_in_docx 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.style import table_style
from app.tools.effective_date import get_next_day from app.tools.effective_date import get_next_day
from app.tools.find_before_word import extract_overload_info_from_previous_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 file_path_dict["power_off_excel"] = folder_path + "/" + file
continue 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("文件格式错误") logger.exception("文件格式错误")
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,
@ -104,13 +108,29 @@ def deal_docx(folder_path, save_path=None, time_type=0):
) )
continue continue
if "first_point_para2" not in doc_dict:
if re.match(r".*抢修工单.*", para.text):
# print(para.text)
doc_dict["first_point_para2"] = re.sub(
r"[,]整体抢修工作态势正常。+$", "", para.text
)
continue
# 第二点过载台数 # 第二点过载台数
if re.search(r"过载(\d+)(?:[条台]|\s*)?", para.text):
doc_dict["over_load"] = (
re.search(r"过载(\d+)(?:[条台]|\s*)?", para.text).group().replace("过载", "")
).replace("", "").replace("", "")
# print(doc_dict['over_load'])
continue
"""
if re.search(r"过载\d+台", para.text): if re.search(r"过载\d+台", para.text):
doc_dict["over_load"] = ( doc_dict["over_load"] = (
re.search(r"过载\d+台", para.text).group().replace("过载", "") re.search(r"过载\d+台", para.text).group().replace("过载", "")
).replace("", "") ).replace("", "")
# print(doc_dict['over_load']) # print(doc_dict['over_load'])
continue continue
"""
# 拿到舆情的段落 # 拿到舆情的段落
paragraphs_sentiment = doc_sentiment.paragraphs paragraphs_sentiment = doc_sentiment.paragraphs
@ -137,11 +157,12 @@ def deal_docx(folder_path, save_path=None, time_type=0):
table1 = tables[0] table1 = tables[0]
# 表2配变过载监测汇总表 # 表2配变过载监测汇总表——配变过载集成到了一张表table1
table2 = tables[1] #table2 = tables[1]
# 表3停电用户前五供电局 # 表3停电用户前五供电局——停电前五移动到了第二张表
table3 = tables[2] table3 = tables[1]
#table3 = tables[2]
# 新增表4 95598供电类投诉前五供电局统计表 # 新增表4 95598供电类投诉前五供电局统计表
# table4 = doc_poweroff.add_table(6, 5) # table4 = doc_poweroff.add_table(6, 5)
@ -348,10 +369,10 @@ def deal_docx(folder_path, save_path=None, time_type=0):
standardize_date = ( standardize_date = (
f"{month_before}{day_before}日17时至{month}{day}日17时期间" f"{month_before}{day_before}日17时至{month}{day}日17时期间"
) )
elif time_type == 1: elif time_type == 1:
# -------------------------20250429更新修改开始和结束时间--------------------------------- # -------------------------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 = '' # standardize_date = ''
# -------------------------20250429更新修改开始和结束时间--------------------------------- # -------------------------20250429更新修改开始和结束时间---------------------------------
@ -424,6 +445,8 @@ def deal_docx(folder_path, save_path=None, time_type=0):
current_doc_name = f"南方电网公司停电抢修投诉服务舆情管控三工单联动监测日报{year}{int(month):02d}{int(day):02d}.docx" current_doc_name = f"南方电网公司停电抢修投诉服务舆情管控三工单联动监测日报{year}{int(month):02d}{int(day):02d}.docx"
if "over_load" not in doc_dict:
raise Exception("过载数据不符合标准未解析,请查看文档")
doc_dict_over_load = doc_dict["over_load"] doc_dict_over_load = doc_dict["over_load"]
over_load_before = extract_overload_info_from_previous_day( over_load_before = extract_overload_info_from_previous_day(
current_word=current_doc_name current_word=current_doc_name
@ -577,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["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}}条; # {{standardize_date}}全网收到{{complain_num}}条供电类投诉,环比{{complain_dod}}条;
complain_text = ( complain_text = (
@ -678,21 +702,20 @@ def deal_docx(folder_path, save_path=None, time_type=0):
# 复制表1的数据 # 复制表1的数据
logger.info("将配变过载写入表0第二行开始第9列开始") logger.info("将配变过载写入表0第二行开始第9列开始")
# 定义要查看的区域范围 # 定义要查看的区域范围
start_row2 = 1 # 起始行索引从0开始 start_row2 = 2 # 起始行索引从0开始
end_row2 = 7 # 结束行索引(不包括) end_row2 = 8 # 结束行索引(不包括)
start_col2 = 2 # 起始列索引从0开始 start_col2 = 9 # 起始列索引从0开始
end_col2 = 4 # 结束列索引(不包括) end_col2 = 11 # 结束列索引(不包括)
target_row=1 #target_row=1
target_col=7 #target_col=7
copy_table_no_align( copy_table(
table2, table1,
electricity_daily.tables[0], electricity_daily.tables[0],
start_row2, start_row2,
end_row2, end_row2,
start_col2, start_col2,
end_col2, end_col2,
target_row, 0
target_col
) )
""" """
copy_table( copy_table(
@ -748,6 +771,21 @@ def deal_docx(folder_path, save_path=None, time_type=0):
is_dynamics=False 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) # copy_sta_table(electricity_daily.tables[3], top5_list, start_tb4_row, start_tb4_col)
# 将表格中的字体中文设置成仿宋英文数字设置成新罗马均为11号大小 # 将表格中的字体中文设置成仿宋英文数字设置成新罗马均为11号大小

@ -75,12 +75,13 @@ def extract_overload_info_from_previous_day(
# 提取过载台数信息 # 提取过载台数信息
overload_info = None overload_info = None
for para in doc.paragraphs: for para in doc.paragraphs:
if re.search(r"过载\d+", para.text): if re.search(r"过载(\d+)[条]", para.text):
overload_info = ( overload_info = (
re.search(r"过载\d+", para.text) re.search(r"过载(\d+)[条]", para.text)
.group() .group()
.replace("过载", "") .replace("过载", "")
.replace("", "") .replace("", "")
.replace("", "")
) )
break break

@ -90,4 +90,4 @@ def copy_sta_table(
target_table.cell(target_row, target_col).text = str(data[i][j]) target_table.cell(target_row, target_col).text = str(data[i][j])
except Exception as e: except Exception as e:
logger.exception(f"自行统计的数据插入word表格中失败: {e}") logger.exception(f"自行统计的数据插入word表格中失败: {e}")
raise e raise e

@ -1,4 +1,12 @@
# 2025-07-11 # 2025-07-11
1) 日报模板 1) 日报模板
2) tools/final_doc.py 2) tools/final_doc.py
3) tools/replace_table.py 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
Loading…
Cancel
Save