|
@@ -12,7 +12,7 @@ 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, Cm
|
|
|
+from docx.shared import Inches, Cm, Pt
|
|
|
|
|
|
from commom import GeneralException, f_get_datetime
|
|
|
from config import BaseConfig
|
|
@@ -23,32 +23,37 @@ 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)
|
|
|
+ def _set_cell_width(table, table_cell_width):
|
|
|
+ for column in table.columns:
|
|
|
+ if table_cell_width is not None:
|
|
|
+ column.width = Cm(table_cell_width)
|
|
|
+ # elif 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
|
|
|
+ def _set_cell_format(cell, font_size=None):
|
|
|
+ for paragraph in cell.paragraphs:
|
|
|
+ paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
|
+ for run in paragraph.runs:
|
|
|
+ if font_size is not None:
|
|
|
+ run.font.size = Pt(font_size)
|
|
|
cell.vertical_alignment = WD_ALIGN_VERTICAL.CENTER
|
|
|
|
|
|
@staticmethod
|
|
|
- def _merge_cell_column(pre_cell, curr_cell):
|
|
|
+ def _merge_cell_column(pre_cell, curr_cell, table_font_size, table_cell_width):
|
|
|
if curr_cell.text == pre_cell.text:
|
|
|
column_name = curr_cell.text
|
|
|
pre_cell.merge(curr_cell)
|
|
|
pre_cell.text = column_name
|
|
|
for run in pre_cell.paragraphs[0].runs:
|
|
|
run.bold = True
|
|
|
- Report._set_cell_format(pre_cell)
|
|
|
- Report._set_cell_width(pre_cell)
|
|
|
+ Report._set_cell_format(pre_cell, table_font_size)
|
|
|
|
|
|
@staticmethod
|
|
|
def _set_table_singleBoard(table):
|
|
@@ -115,6 +120,9 @@ class Report():
|
|
|
for metric_code, metric_fuc_entity in metric_value_dict.items():
|
|
|
placeholder = Report._get_placeholder(PlaceholderPrefixEnum.TABLE, metric_code)
|
|
|
metric_table = metric_fuc_entity.table
|
|
|
+ table_font_size = metric_fuc_entity.table_font_size
|
|
|
+ table_autofit = metric_fuc_entity.table_autofit
|
|
|
+ table_cell_width = metric_fuc_entity.table_cell_width
|
|
|
if metric_table is None:
|
|
|
continue
|
|
|
if not placeholder in paragraph.text:
|
|
@@ -131,26 +139,26 @@ class Report():
|
|
|
cell.text = str(column_name)
|
|
|
for run in cell.paragraphs[0].runs:
|
|
|
run.bold = True
|
|
|
- Report._set_cell_format(cell)
|
|
|
- Report._set_cell_width(cell)
|
|
|
+ Report._set_cell_format(cell, table_font_size)
|
|
|
# 合并相同的列名
|
|
|
if column_idx != 0 and BaseConfig.merge_table_column:
|
|
|
pre_cell = table.cell(0, column_idx - 1)
|
|
|
- Report._merge_cell_column(pre_cell, cell)
|
|
|
+ Report._merge_cell_column(pre_cell, cell, table_font_size, table_cell_width)
|
|
|
# 值
|
|
|
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)
|
|
|
- Report._set_cell_format(cell)
|
|
|
- Report._set_cell_width(cell)
|
|
|
+ Report._set_cell_format(cell, table_font_size)
|
|
|
# 合并第一行数据也为列的情况
|
|
|
if row_idx == 0:
|
|
|
- Report._merge_cell_column(table.cell(0, column_idx), cell)
|
|
|
+ Report._merge_cell_column(table.cell(0, column_idx), cell, table_font_size,
|
|
|
+ table_cell_width)
|
|
|
|
|
|
+ Report._set_cell_width(table, table_cell_width)
|
|
|
Report._set_table_singleBoard(table)
|
|
|
# 禁止自动调整表格
|
|
|
- if len(metric_table.columns) <= 12:
|
|
|
+ if len(metric_table.columns) <= 12 or not table_autofit:
|
|
|
table.autofit = False
|
|
|
|
|
|
@staticmethod
|
|
@@ -179,7 +187,7 @@ class Report():
|
|
|
run.add_picture(path, width=Inches(image_size))
|
|
|
|
|
|
@staticmethod
|
|
|
- def generate_report(metric_value_dict: Dict[str, MetricFucEntity], template_path: str, path=None):
|
|
|
+ def generate_report(metric_value_dict: Dict[str, MetricFucEntity], template_path: str, save_path=None):
|
|
|
if os.path.exists(template_path):
|
|
|
doc = Document(template_path)
|
|
|
else:
|
|
@@ -189,8 +197,8 @@ class Report():
|
|
|
Report._fill_table_placeholder(doc, metric_value_dict)
|
|
|
Report._fill_image_placeholder(doc, metric_value_dict)
|
|
|
new_path = template_path.replace(".docx", f"{f_get_datetime()}.docx")
|
|
|
- if path is not None:
|
|
|
- new_path = path
|
|
|
+ if save_path is not None:
|
|
|
+ new_path = save_path
|
|
|
doc.save(f"./{new_path}")
|
|
|
|
|
|
|