def format_space_group(paragraph: Paragraph, space_group: str, it_number: str) -> None: """ Sets formatting of the space group symbol in row 6 of the report table. """ try: # The HM space group type s = SpaceGroups() spgrxml = s.to_mathml(space_group) paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT paragraph._element.append(math_to_word(spgrxml)) paragraph.add_run(' (' + it_number + ')') except Exception: paragraph.add_run(space_group)
def _render_element(self, p: Paragraph, element: str or Element, is_root=False, bold=False, italic=False, strike=False, underline=False, font_size=None, sup=False, sub=False): """ 转换html节点到word :param element: :return: """ if isinstance(element, str): run = p.add_run(self._clear_text(element)) run.bold = bold run.italic = italic run.font.strike = strike run.font.underline = underline run.font.subscript = sub run.font.superscript = sup if font_size: run.font.size = font_size self.__force_simsun(run) return pq = PyQuery(element) if pq.is_('p'): # 不支持嵌套p,自动扁平化 contents = pq.contents() align = self._get_pq_style(pq, 'text-align') if align == 'center': p.alignment = WD_ALIGN_PARAGRAPH.CENTER elif align == 'right': p.alignment = WD_ALIGN_PARAGRAPH.RIGHT else: p.alignment = WD_ALIGN_PARAGRAPH.LEFT if is_root: self._render_children(p, contents) else: sub_p = p._parent.add_paragraph() if align == 'center': sub_p.alignment = WD_ALIGN_PARAGRAPH.CENTER elif align == 'right': sub_p.alignment = WD_ALIGN_PARAGRAPH.RIGHT else: sub_p.alignment = WD_ALIGN_PARAGRAPH.LEFT self._render_children(sub_p, contents) elif pq.is_('u'): # 下划线 self.__render_inline_element(p, pq, underline=True, bold=bold, italic=italic, strike=strike, font_size=font_size, sub=sub, sup=sup) elif pq.is_('strong') or pq.is_('b'): # 加粗 self.__render_inline_element(p, pq, underline=underline, bold=True, italic=italic, strike=strike, font_size=font_size, sub=sub, sup=sup) elif pq.is_('i') or pq.is_('em'): # 斜体 self.__render_inline_element(p, pq, underline=underline, bold=bold, italic=True, strike=strike, font_size=font_size, sub=sub, sup=sup) elif pq.is_('sub'): # 下标 self.__render_inline_element(p, pq, underline=underline, bold=bold, italic=italic, strike=strike, font_size=font_size, sub=True, sup=sup) elif pq.is_('sup'): # 上标 self.__render_inline_element(p, pq, underline=underline, bold=bold, italic=italic, strike=strike, font_size=font_size, sub=sub, sup=True) elif pq.is_('var'): # 老公式 self.__render_inline_element(p, pq, underline=underline, bold=bold, italic=True, strike=strike, font_size=font_size, sub=sub, sup=sup) elif pq.is_('span'): self._render_span(p, pq, bold=bold, italic=italic, strike=strike, underline=underline, font_size=font_size) elif pq.is_("br"): p.add_run().add_break() elif pq.is_("div"): # sub_p = p._parent.add_paragraph() p.add_run().add_break() self._render_children(p, pq.contents()) elif pq.is_('ul'): self._render_unorder_list(p, pq) elif pq.is_('ol'): self._render_order_list(p, pq) elif pq.is_('table'): self._render_table(p, pq) elif pq.is_('img'): # 图片 self._render_img(p, pq) elif element.tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'): sub_p = p._parent.add_paragraph() self.__render_inline_element(sub_p, pq, bold=True, font_size=Pt(12), underline=underline, italic=True, strike=strike, sub=sub, sup=sup) else: sub_p = p._parent.add_paragraph() contents = pq.contents() self._render_children(sub_p, contents)