|
@@ -7,11 +7,11 @@
|
|
|
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 commom import GeneralException, f_get_datetime
|
|
|
+from entitys import MetricFucEntity
|
|
|
from enums import ResultCodesEnum, PlaceholderPrefixEnum
|
|
|
|
|
|
|
|
@@ -40,47 +40,48 @@ class Report():
|
|
|
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:
|
|
|
+ if metric_table.empty:
|
|
|
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
|
|
|
+ if not placeholder in paragraph.text:
|
|
|
+ continue
|
|
|
+ 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
|
|
|
- # 值
|
|
|
- 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 _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):
|
|
|
+ def generate_report(metric_value_dict: Dict[str, MetricFucEntity], template_path: str):
|
|
|
if os.path.exists(template_path):
|
|
|
doc = Document(template_path)
|
|
|
else:
|
|
@@ -88,9 +89,9 @@ class Report():
|
|
|
|
|
|
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}_")
|
|
|
+ # Report._fill_image_placeholder(doc, metric_value_dict)
|
|
|
+ new_path = template_path.replace(".docx", f"{f_get_datetime()}.docx")
|
|
|
+ doc.save(f"./{new_path}")
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|