report_generate.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: yq
  4. @time: 2024/11/8
  5. @desc:
  6. """
  7. import os
  8. from typing import Dict
  9. import pandas as pd
  10. from docx import Document
  11. from docx.enum.table import WD_ALIGN_VERTICAL
  12. from docx.enum.text import WD_ALIGN_PARAGRAPH
  13. from docx.oxml import OxmlElement
  14. from docx.oxml.ns import qn
  15. from docx.shared import Inches, Cm, Pt
  16. from commom import GeneralException, f_get_datetime
  17. from config import BaseConfig
  18. from entitys import MetricFucEntity
  19. from enums import ResultCodesEnum, PlaceholderPrefixEnum
  20. class Report():
  21. @staticmethod
  22. def _set_cell_width(table, table_cell_width):
  23. for column in table.columns:
  24. if table_cell_width is not None:
  25. column.width = Cm(table_cell_width)
  26. continue
  27. max_text_len = 0
  28. for cell in column.cells:
  29. max_text_len = len(cell.text) if len(cell.text) > max_text_len else max_text_len
  30. if max_text_len >= 10:
  31. column.width = Cm(2)
  32. elif max_text_len >= 15:
  33. column.width = Cm(2.5)
  34. elif max_text_len >= 25:
  35. column.width = Cm(3)
  36. else:
  37. column.width = Cm(1.5)
  38. @staticmethod
  39. def _set_cell_format(cell, font_size=None):
  40. for paragraph in cell.paragraphs:
  41. # paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
  42. for run in paragraph.runs:
  43. # 判断文本是否包含中文
  44. if any('\u4e00' <= char <= '\u9fff' for char in run.text):
  45. run.font.name = '宋体' # 设置中文字体为宋体
  46. else:
  47. run.font.name = 'Times New Roman' # 设置英文字体为Times New Roman
  48. if font_size is not None:
  49. run.font.size = Pt(font_size)
  50. cell.vertical_alignment = WD_ALIGN_VERTICAL.CENTER
  51. @staticmethod
  52. def _merge_cell_column(pre_cell, curr_cell, table_font_size, table_cell_width):
  53. if curr_cell.text == pre_cell.text:
  54. column_name = curr_cell.text
  55. pre_cell.merge(curr_cell)
  56. pre_cell.text = column_name
  57. for run in pre_cell.paragraphs[0].runs:
  58. run.bold = True
  59. Report._set_cell_format(pre_cell, table_font_size)
  60. @staticmethod
  61. def _set_table_singleBoard(table):
  62. # 将table 的所有单元格四个边设置为 0.5 镑, 黑色, 实线
  63. def _set_table_boarder(table, **kwargs):
  64. """
  65. Set table`s border
  66. Usage:
  67. set_table_border(
  68. cell,
  69. top={"sz": 12, "val": "single", "color": "#FF0000"},
  70. bottom={"sz": 12, "color": "#00FF00", "val": "single"},
  71. left={"sz": 24, "val": "dashed"},
  72. right={"sz": 12, "val": "dashed"},
  73. )
  74. """
  75. borders = OxmlElement('w:tblBorders')
  76. for tag in ('bottom', 'top', 'left', 'right', 'insideV', 'insideH'):
  77. edge_data = kwargs.get(tag)
  78. if edge_data:
  79. any_border = OxmlElement(f'w:{tag}')
  80. for key in ["sz", "val", "color", "space", "shadow"]:
  81. if key in edge_data:
  82. any_border.set(qn(f'w:{key}'), str(edge_data[key]))
  83. borders.append(any_border)
  84. table._tbl.tblPr.append(borders)
  85. return _set_table_boarder(
  86. table,
  87. top={"sz": 4, "val": "single", "color": "#000000"},
  88. bottom={"sz": 4, "val": "single", "color": "#000000"},
  89. left={"sz": 4, "val": "single", "color": "#000000"},
  90. right={"sz": 4, "val": "single", "color": "#000000"},
  91. insideV={"sz": 4, "val": "single", "color": "#000000"},
  92. insideH={"sz": 4, "val": "single", "color": "#000000"}
  93. )
  94. @staticmethod
  95. def _get_placeholder(placeholder_prefix_enum: PlaceholderPrefixEnum, metric_code: str):
  96. return "{{" + f"{placeholder_prefix_enum.value}{metric_code}" + "}}"
  97. @staticmethod
  98. def _fill_value_placeholder(doc: Document, metric_value_dict: Dict[str, MetricFucEntity]):
  99. # 替换指标
  100. for paragraph in doc.paragraphs:
  101. text = paragraph.text
  102. for metric_code, metric_fuc_entity in metric_value_dict.items():
  103. placeholder = Report._get_placeholder(PlaceholderPrefixEnum.VALUE, metric_code)
  104. metric_value = metric_fuc_entity.value
  105. if metric_value is None:
  106. continue
  107. text = text.replace(placeholder, str(metric_value))
  108. # 段落中多个runs时执行,最后一个run改成替换好的文本,其他run置空
  109. if len(paragraph.runs[:-1]) > 0:
  110. for run in paragraph.runs[:-1]:
  111. run.text = ''
  112. paragraph.runs[-1].text = text
  113. @staticmethod
  114. def _get_text_length(text):
  115. return sum(3 if '\u4e00' <= char <= '\u9fff' else 1 for char in text)
  116. @staticmethod
  117. def _fill_table_placeholder(doc: Document, metric_value_dict: Dict[str, MetricFucEntity]):
  118. # 替换表格
  119. for paragraph in doc.paragraphs:
  120. for metric_code, metric_fuc_entity in metric_value_dict.items():
  121. placeholder = Report._get_placeholder(PlaceholderPrefixEnum.TABLE, metric_code)
  122. metric_table = metric_fuc_entity.table
  123. table_font_size = metric_fuc_entity.table_font_size
  124. table_autofit = metric_fuc_entity.table_autofit
  125. table_cell_width = metric_fuc_entity.table_cell_width
  126. if metric_table is None:
  127. continue
  128. if not placeholder in paragraph.text:
  129. continue
  130. # 清除占位符
  131. for run in paragraph.runs:
  132. run.text = run.text.replace(placeholder, "")
  133. table = doc.add_table(rows=metric_table.shape[0] + 1, cols=metric_table.shape[1])
  134. table.alignment = WD_ALIGN_PARAGRAPH.CENTER
  135. paragraph._element.addnext(table._element)
  136. # 根据列名计算单元格宽度,对不符合最小宽度的情况,重新调整
  137. # TODO:根据列名和内容综合调整单元格宽度
  138. a4_width = 21 - 2 * 3.18
  139. total_columns = metric_table.shape[1]
  140. col_lengthes = [Report._get_text_length(c) for c in metric_table.columns]
  141. cell_width_unit = a4_width / sum(col_lengthes)
  142. cell_widths = [c * cell_width_unit for c in col_lengthes]
  143. min_cell_width = 1
  144. adjusted_cell_widths = [max(c, min_cell_width) for c in cell_widths]
  145. adjusted_width = sum(adjusted_cell_widths)
  146. if adjusted_width > a4_width:
  147. excess_width = adjusted_width - a4_width
  148. excess_width_per_column = excess_width / total_columns
  149. adjusted_cell_widths = [max(min_cell_width, c - excess_width_per_column) for c in
  150. adjusted_cell_widths]
  151. # 列名
  152. for column_idx, column_name in enumerate(metric_table.columns):
  153. cell = table.cell(0, column_idx)
  154. cell.text = str(column_name)
  155. for run in cell.paragraphs[0].runs:
  156. run.bold = True
  157. Report._set_cell_format(cell, table_font_size)
  158. table.columns[column_idx].width = Cm(adjusted_cell_widths[column_idx])
  159. # 合并相同的列名
  160. if column_idx != 0 and BaseConfig.merge_table_column:
  161. pre_cell = table.cell(0, column_idx - 1)
  162. Report._merge_cell_column(pre_cell, cell, table_font_size, table_cell_width)
  163. # 值
  164. for row_idx, row in metric_table.iterrows():
  165. for column_idx, value in enumerate(row):
  166. cell = table.cell(row_idx + 1, column_idx)
  167. if "率" in metric_table.columns[column_idx] or (
  168. "率" in str(row[0]) and pd.notna(value) and (column_idx != 0)):
  169. value = f"{float(value) * 100:.2f}%" if pd.notna(value) else '/'
  170. else:
  171. value = str(value) if pd.notna(value) else '/'
  172. cell.text = str(value)
  173. Report._set_cell_format(cell, table_font_size)
  174. # 合并第一行数据也为列的情况
  175. if row_idx == 0:
  176. Report._merge_cell_column(table.cell(0, column_idx), cell, table_font_size,
  177. table_cell_width)
  178. # Report._set_cell_width(table, table_cell_width)
  179. Report._set_table_singleBoard(table)
  180. # 禁止自动调整表格
  181. if len(metric_table.columns) <= 12 or not table_autofit:
  182. table.autofit = False
  183. @staticmethod
  184. def _fill_image_placeholder(doc: Document, metric_value_dict: Dict[str, MetricFucEntity]):
  185. # 替换图片
  186. for paragraph in doc.paragraphs:
  187. for metric_code, metric_fuc_entity in metric_value_dict.items():
  188. placeholder = Report._get_placeholder(PlaceholderPrefixEnum.IMAGE, metric_code)
  189. image_path = metric_fuc_entity.image_path
  190. image_size = metric_fuc_entity.image_size
  191. if image_path is None:
  192. continue
  193. if not placeholder in paragraph.text:
  194. continue
  195. if isinstance(image_path, str):
  196. image_path = [image_path]
  197. for path in image_path:
  198. if not os.path.exists(path):
  199. raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"文件【{image_path}】不存在")
  200. # 清除占位符
  201. for run in paragraph.runs:
  202. if placeholder not in run.text:
  203. continue
  204. run.text = run.text.replace(placeholder, "")
  205. for path in image_path:
  206. run.add_picture(path, width=Inches(image_size))
  207. @staticmethod
  208. def generate_report(metric_value_dict: Dict[str, MetricFucEntity], template_path: str, save_path=None):
  209. if os.path.exists(template_path):
  210. doc = Document(template_path)
  211. else:
  212. raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"监控模板文件【{template_path}】不存在")
  213. Report._fill_value_placeholder(doc, metric_value_dict)
  214. Report._fill_table_placeholder(doc, metric_value_dict)
  215. Report._fill_image_placeholder(doc, metric_value_dict)
  216. new_path = template_path.replace(".docx", f"{f_get_datetime()}.docx")
  217. if save_path is not None:
  218. new_path = save_path
  219. doc.save(f"./{new_path}")
  220. if __name__ == "__main__":
  221. pass