Loading generateBaseline/postprocessing.py +16 −9 Changes for generateBaseline/postprocessing.py: 16 added lines, 9 removed lines. Original line number Diff line number Diff line Loading @@ -2937,14 +2937,11 @@ def add_break_after_code_blocks_and_tables(docx_input, docx_output): def update_equation_style(docx_input, docx_output): """ Updates equation styles from 'Equation' to 'EQ' in a DOCX file. Apply EQ paragraph style to display equations only. Parameters ---------- docx_input : str Path to the input DOCX file. docx_output : str Path to the output DOCX file. Equation-only paragraphs (no surrounding body text) get w:pStyle EQ. Inline equations within text are left unchanged (paragraph keeps Normal or whatever body style it already has; EQ is a paragraph style only). """ ns = { "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main", Loading @@ -2958,17 +2955,27 @@ def update_equation_style(docx_input, docx_output): root = etree.fromstring(xml_data) new_style = "EQ" counter = 0 EQUATION_ELEMENT_WRAPPED_IN_RUN = False # Loop over all elements to find "Equation" and change to "EQ" for equation in root.xpath('.//m:oMath', namespaces=ns): p = equation.getparent() if p.tag == f"{{{ns['w']}}}r": # Equation element wrapped in a run EQUATION_ELEMENT_WRAPPED_IN_RUN = True else: EQUATION_ELEMENT_WRAPPED_IN_RUN = False while p is not None and p.tag != f"{{{ns['w']}}}p": p = p.getparent() if p is None: continue pStyle = p.xpath('./w:pPr/w:pStyle', namespaces=ns) if pStyle: # Check whether the equation element is the only element in the paragraph. The equation element may be not wrapped in a run. if (EQUATION_ELEMENT_WRAPPED_IN_RUN and len(p.xpath('.//w:r', namespaces=ns)) == 1) or (not EQUATION_ELEMENT_WRAPPED_IN_RUN and len(p.xpath('.//w:r', namespaces=ns)) == 0): pStyle[0].set(f"{{{ns['w']}}}val", new_style) counter += 1 # If other runs are present, meaning it is inline equation, no need to modify anything. continue print(f'Changed style "Equation" to "EQ" {counter} times') xml_data = etree.tostring(root, xml_declaration=True, encoding="UTF-8", standalone="yes") Loading Loading
generateBaseline/postprocessing.py +16 −9 Changes for generateBaseline/postprocessing.py: 16 added lines, 9 removed lines. Original line number Diff line number Diff line Loading @@ -2937,14 +2937,11 @@ def add_break_after_code_blocks_and_tables(docx_input, docx_output): def update_equation_style(docx_input, docx_output): """ Updates equation styles from 'Equation' to 'EQ' in a DOCX file. Apply EQ paragraph style to display equations only. Parameters ---------- docx_input : str Path to the input DOCX file. docx_output : str Path to the output DOCX file. Equation-only paragraphs (no surrounding body text) get w:pStyle EQ. Inline equations within text are left unchanged (paragraph keeps Normal or whatever body style it already has; EQ is a paragraph style only). """ ns = { "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main", Loading @@ -2958,17 +2955,27 @@ def update_equation_style(docx_input, docx_output): root = etree.fromstring(xml_data) new_style = "EQ" counter = 0 EQUATION_ELEMENT_WRAPPED_IN_RUN = False # Loop over all elements to find "Equation" and change to "EQ" for equation in root.xpath('.//m:oMath', namespaces=ns): p = equation.getparent() if p.tag == f"{{{ns['w']}}}r": # Equation element wrapped in a run EQUATION_ELEMENT_WRAPPED_IN_RUN = True else: EQUATION_ELEMENT_WRAPPED_IN_RUN = False while p is not None and p.tag != f"{{{ns['w']}}}p": p = p.getparent() if p is None: continue pStyle = p.xpath('./w:pPr/w:pStyle', namespaces=ns) if pStyle: # Check whether the equation element is the only element in the paragraph. The equation element may be not wrapped in a run. if (EQUATION_ELEMENT_WRAPPED_IN_RUN and len(p.xpath('.//w:r', namespaces=ns)) == 1) or (not EQUATION_ELEMENT_WRAPPED_IN_RUN and len(p.xpath('.//w:r', namespaces=ns)) == 0): pStyle[0].set(f"{{{ns['w']}}}val", new_style) counter += 1 # If other runs are present, meaning it is inline equation, no need to modify anything. continue print(f'Changed style "Equation" to "EQ" {counter} times') xml_data = etree.tostring(root, xml_declaration=True, encoding="UTF-8", standalone="yes") Loading