|
@@ -14,41 +14,51 @@ from docx.oxml.ns import qn
|
|
|
from docx.shared import Inches
|
|
|
|
|
|
from commom import GeneralException, f_get_datetime
|
|
|
+from config import BaseConfig
|
|
|
from entitys import MetricFucEntity
|
|
|
from enums import ResultCodesEnum, PlaceholderPrefixEnum
|
|
|
|
|
|
|
|
|
class Report():
|
|
|
|
|
|
- # 设置 table 的边框,用法与 cell 类似
|
|
|
@staticmethod
|
|
|
- def _set_table_boarder(table, **kwargs):
|
|
|
- """
|
|
|
- Set table`s border
|
|
|
- Usage:
|
|
|
- set_table_border(
|
|
|
- cell,
|
|
|
- top={"sz": 12, "val": "single", "color": "#FF0000"},
|
|
|
- bottom={"sz": 12, "color": "#00FF00", "val": "single"},
|
|
|
- left={"sz": 24, "val": "dashed"},
|
|
|
- right={"sz": 12, "val": "dashed"},
|
|
|
- )
|
|
|
- """
|
|
|
- borders = OxmlElement('w:tblBorders')
|
|
|
- for tag in ('bottom', 'top', 'left', 'right', 'insideV', 'insideH'):
|
|
|
- edge_data = kwargs.get(tag)
|
|
|
- if edge_data:
|
|
|
- any_border = OxmlElement(f'w:{tag}')
|
|
|
- for key in ["sz", "val", "color", "space", "shadow"]:
|
|
|
- if key in edge_data:
|
|
|
- any_border.set(qn(f'w:{key}'), str(edge_data[key]))
|
|
|
- borders.append(any_border)
|
|
|
- table._tbl.tblPr.append(borders)
|
|
|
-
|
|
|
- # 将table 的所有单元格四个边设置为 0.5 镑, 黑色, 实线
|
|
|
+ def _merge_cell_column(pre_cell, curr_cell):
|
|
|
+ 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
|
|
|
+ pre_cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
|
+
|
|
|
@staticmethod
|
|
|
def _set_table_singleBoard(table):
|
|
|
- return Report._set_table_boarder(
|
|
|
+ # 将table 的所有单元格四个边设置为 0.5 镑, 黑色, 实线
|
|
|
+
|
|
|
+ def _set_table_boarder(table, **kwargs):
|
|
|
+ """
|
|
|
+ Set table`s border
|
|
|
+ Usage:
|
|
|
+ set_table_border(
|
|
|
+ cell,
|
|
|
+ top={"sz": 12, "val": "single", "color": "#FF0000"},
|
|
|
+ bottom={"sz": 12, "color": "#00FF00", "val": "single"},
|
|
|
+ left={"sz": 24, "val": "dashed"},
|
|
|
+ right={"sz": 12, "val": "dashed"},
|
|
|
+ )
|
|
|
+ """
|
|
|
+ borders = OxmlElement('w:tblBorders')
|
|
|
+ for tag in ('bottom', 'top', 'left', 'right', 'insideV', 'insideH'):
|
|
|
+ edge_data = kwargs.get(tag)
|
|
|
+ if edge_data:
|
|
|
+ any_border = OxmlElement(f'w:{tag}')
|
|
|
+ for key in ["sz", "val", "color", "space", "shadow"]:
|
|
|
+ if key in edge_data:
|
|
|
+ any_border.set(qn(f'w:{key}'), str(edge_data[key]))
|
|
|
+ borders.append(any_border)
|
|
|
+ table._tbl.tblPr.append(borders)
|
|
|
+
|
|
|
+ return _set_table_boarder(
|
|
|
table,
|
|
|
top={"sz": 4, "val": "single", "color": "#000000"},
|
|
|
bottom={"sz": 4, "val": "single", "color": "#000000"},
|
|
@@ -98,11 +108,17 @@ class Report():
|
|
|
paragraph._element.addnext(table._element)
|
|
|
# 列名
|
|
|
for column_idx, column_name in enumerate(metric_table.columns):
|
|
|
+ if column_name == "c3":
|
|
|
+ column_name = "c2"
|
|
|
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
|
|
|
+ # 合并相同的列名
|
|
|
+ if column_idx != 0 and BaseConfig.merge_table_column:
|
|
|
+ pre_cell = table.cell(0, column_idx - 1)
|
|
|
+ Report._merge_cell_column(pre_cell, cell)
|
|
|
# 值
|
|
|
for row_idx, row in metric_table.iterrows():
|
|
|
for column_idx, value in enumerate(row):
|