|
@@ -0,0 +1,97 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+"""
|
|
|
+@author: yq
|
|
|
+@time: 2024/11/8
|
|
|
+@desc:
|
|
|
+"""
|
|
|
+import os
|
|
|
+from typing import Dict
|
|
|
+
|
|
|
+from commom import GeneralException
|
|
|
+from entitys import MetricFucEntity
|
|
|
+from docx import Document
|
|
|
+from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
|
+
|
|
|
+from enums import ResultCodesEnum, PlaceholderPrefixEnum
|
|
|
+
|
|
|
+
|
|
|
+class Report():
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def _fill_value_placeholder(doc: Document, metric_value_dict: Dict[str, MetricFucEntity]):
|
|
|
+ # 替换指标
|
|
|
+ for paragraph in doc.paragraphs:
|
|
|
+ text = paragraph.text
|
|
|
+ for metric_code, metric_fuc_entity in metric_value_dict.items():
|
|
|
+ placeholder = f"{PlaceholderPrefixEnum.VALUE.value}_{metric_code}"
|
|
|
+ metric_value = metric_fuc_entity.value
|
|
|
+ if metric_value:
|
|
|
+ text = text.replace(placeholder, metric_value)
|
|
|
+ # 段落中多个runs时执行,最后一个run改成替换好的文本,其他run置空
|
|
|
+ if len(paragraph.runs[:-1]) > 0:
|
|
|
+ for run in paragraph.runs[:-1]:
|
|
|
+ run.text = ''
|
|
|
+ paragraph.runs[-1].text = text
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def _fill_table_placeholder(doc: Document, metric_value_dict: Dict[str, MetricFucEntity]):
|
|
|
+ # 替换表格
|
|
|
+ for paragraph in doc.paragraphs:
|
|
|
+ for metric_code, metric_fuc_entity in metric_value_dict.items():
|
|
|
+ placeholder = f"{PlaceholderPrefixEnum.TABLE.value}_{metric_code}"
|
|
|
+ metric_table = metric_fuc_entity.table
|
|
|
+ if not metric_table:
|
|
|
+ continue
|
|
|
+ # 清除占位符
|
|
|
+ if placeholder in paragraph.text:
|
|
|
+ for run in paragraph.runs:
|
|
|
+ run.text = run.text.replace(placeholder, "")
|
|
|
+ table = doc.add_table(rows=metric_table.shape[0] + 1, cols=metric_table.shape[1])
|
|
|
+ table.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
|
+ paragraph._element.addnext(table._element)
|
|
|
+ # 列名
|
|
|
+ for column_idx, column_name in enumerate(metric_table.columns):
|
|
|
+ cell = table.cell(0, column_idx)
|
|
|
+ cell.text = str(column_name)
|
|
|
+ for run in cell.paragraphs[0].runs:
|
|
|
+ run.bold = True
|
|
|
+ cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
|
+ # 值
|
|
|
+ for row_idx, row in metric_table.iterrows():
|
|
|
+ for column_idx, value in enumerate(row):
|
|
|
+ cell = table.cell(row_idx + 1, column_idx)
|
|
|
+ cell.text = str(value)
|
|
|
+ cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def _fill_image_placeholder(doc: Document, metric_value_dict: Dict[str, MetricFucEntity]):
|
|
|
+ # 替换图片
|
|
|
+ for paragraph in doc.paragraphs:
|
|
|
+ text = paragraph.text
|
|
|
+ for metric_code, metric_fuc_entity in metric_value_dict.items():
|
|
|
+ placeholder = f"{PlaceholderPrefixEnum.IMAGE.value}_{metric_code}"
|
|
|
+ metric_value = metric_fuc_entity.value
|
|
|
+ if metric_value:
|
|
|
+ text = text.replace(placeholder, metric_value)
|
|
|
+ # 段落中多个runs时执行,最后一个run改成替换好的文本,其他run置空
|
|
|
+ if len(paragraph.runs[:-1]) > 0:
|
|
|
+ for run in paragraph.runs[:-1]:
|
|
|
+ run.text = ''
|
|
|
+ paragraph.runs[-1].text = text
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def report_generate(metric_value_dict: Dict[str, MetricFucEntity], template_path: str):
|
|
|
+ if os.path.exists(template_path):
|
|
|
+ doc = Document(template_path)
|
|
|
+ else:
|
|
|
+ raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"监控模板文件【{template_path}】不存在")
|
|
|
+
|
|
|
+ Report._fill_value_placeholder(doc, metric_value_dict)
|
|
|
+ Report._fill_table_placeholder(doc, metric_value_dict)
|
|
|
+ Report._fill_image_placeholder(doc, metric_value_dict)
|
|
|
+
|
|
|
+ doc.save(f"./{template_path}_")
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ pass
|