|
@@ -8,10 +8,11 @@ import os
|
|
|
from typing import Dict
|
|
|
|
|
|
from docx import Document
|
|
|
+from docx.enum.table import WD_ALIGN_VERTICAL
|
|
|
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
|
from docx.oxml import OxmlElement
|
|
|
from docx.oxml.ns import qn
|
|
|
-from docx.shared import Inches
|
|
|
+from docx.shared import Inches, Cm
|
|
|
|
|
|
from commom import GeneralException, f_get_datetime
|
|
|
from config import BaseConfig
|
|
@@ -21,6 +22,23 @@ from enums import ResultCodesEnum, PlaceholderPrefixEnum
|
|
|
|
|
|
class Report():
|
|
|
|
|
|
+ @staticmethod
|
|
|
+ def _set_cell_width(cell):
|
|
|
+ text = cell.text
|
|
|
+ if len(text) >= 10:
|
|
|
+ cell.width = Cm(2)
|
|
|
+ elif len(text) >= 15:
|
|
|
+ cell.width = Cm(2.5)
|
|
|
+ elif len(text) >= 25:
|
|
|
+ cell.width = Cm(3)
|
|
|
+ else:
|
|
|
+ cell.width = Cm(1.5)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def _set_cell_format(cell):
|
|
|
+ cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
|
+ cell.vertical_alignment = WD_ALIGN_VERTICAL.CENTER
|
|
|
+
|
|
|
@staticmethod
|
|
|
def _merge_cell_column(pre_cell, curr_cell):
|
|
|
if curr_cell.text == pre_cell.text:
|
|
@@ -29,7 +47,8 @@ class Report():
|
|
|
pre_cell.text = column_name
|
|
|
for run in pre_cell.paragraphs[0].runs:
|
|
|
run.bold = True
|
|
|
- pre_cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
|
+ Report._set_cell_format(pre_cell)
|
|
|
+ Report._set_cell_width(pre_cell)
|
|
|
|
|
|
@staticmethod
|
|
|
def _set_table_singleBoard(table):
|
|
@@ -112,7 +131,8 @@ class Report():
|
|
|
cell.text = str(column_name)
|
|
|
for run in cell.paragraphs[0].runs:
|
|
|
run.bold = True
|
|
|
- cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
|
+ Report._set_cell_format(cell)
|
|
|
+ Report._set_cell_width(cell)
|
|
|
# 合并相同的列名
|
|
|
if column_idx != 0 and BaseConfig.merge_table_column:
|
|
|
pre_cell = table.cell(0, column_idx - 1)
|
|
@@ -122,8 +142,16 @@ class Report():
|
|
|
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
|
|
|
+ Report._set_cell_format(cell)
|
|
|
+ Report._set_cell_width(cell)
|
|
|
+ # 合并第一行数据也为列的情况
|
|
|
+ if row_idx == 0:
|
|
|
+ Report._merge_cell_column(table.cell(0, column_idx), cell)
|
|
|
+
|
|
|
Report._set_table_singleBoard(table)
|
|
|
+ # 禁止自动调整表格
|
|
|
+ if len(metric_table.columns) <= 12:
|
|
|
+ table.autofit = False
|
|
|
|
|
|
@staticmethod
|
|
|
def _fill_image_placeholder(doc: Document, metric_value_dict: Dict[str, MetricFucEntity]):
|
|
@@ -143,7 +171,7 @@ class Report():
|
|
|
if placeholder not in run.text:
|
|
|
continue
|
|
|
run.text = run.text.replace(placeholder, "")
|
|
|
- run.add_picture(image_path, width=Inches(5))
|
|
|
+ run.add_picture(image_path, width=Inches(6))
|
|
|
|
|
|
@staticmethod
|
|
|
def generate_report(metric_value_dict: Dict[str, MetricFucEntity], template_path: str):
|