Loading generateBaseline/postprocessing.py +52 −15 Changes for generateBaseline/postprocessing.py: 52 added lines, 15 removed lines. Original line number Diff line number Diff line Loading @@ -2879,6 +2879,24 @@ def update_source_code_style(docx_input, docx_output): root = etree.fromstring(xml_data) new_style = "PL" counter = 0 counterInlineCode = 0 def add_shading_to_element(elem): """Helper function to add shading to an element""" if elem is None: return shd = elem.xpath('./w:shd', namespaces=ns) if not shd: shd_elem = etree.Element(f"{{{ns['w']}}}shd") shd_elem.set(f"{{{ns['w']}}}val", "clear") shd_elem.set(f"{{{ns['w']}}}color", "auto") shd_elem.set(f"{{{ns['w']}}}fill", "F2F2F2") elem.append(shd_elem) else: shd_elem = shd[0] shd_elem.set(f"{{{ns['w']}}}val", "clear") shd_elem.set(f"{{{ns['w']}}}color", "auto") shd_elem.set(f"{{{ns['w']}}}fill", "F2F2F2") def add_borders_to_paragraph(pPr): """Helper function to add borders and background color to a paragraph""" Loading Loading @@ -2915,21 +2933,6 @@ def update_source_code_style(docx_input, docx_output): for attr, value in border_attrs.items(): border_elem[0].set(attr, value) # Add grey background color (shading) shd = pPr.xpath('./w:shd', namespaces=ns) if not shd: shd_elem = etree.Element(f"{{{ns['w']}}}shd") shd_elem.set(f"{{{ns['w']}}}val", "clear") # Clear pattern shd_elem.set(f"{{{ns['w']}}}color", "auto") # Auto text color shd_elem.set(f"{{{ns['w']}}}fill", "F2F2F2") # Light grey background pPr.append(shd_elem) else: # Update existing shading shd_elem = shd[0] shd_elem.set(f"{{{ns['w']}}}val", "clear") shd_elem.set(f"{{{ns['w']}}}color", "auto") shd_elem.set(f"{{{ns['w']}}}fill", "F2F2F2") # Loop over all elements to find "SourceCode" and change to "PL", and add borders for elem in root.xpath('.//w:pStyle[@w:val="SourceCode"]', namespaces=ns): old_val = elem.get(f"{{{ns['w']}}}val") Loading @@ -2939,6 +2942,7 @@ def update_source_code_style(docx_input, docx_output): # Get the paragraph element to add borders pPr = elem.getparent() add_borders_to_paragraph(pPr) add_shading_to_element(pPr) # Remove other rPr:rStyle elements para = pPr.getparent() Loading @@ -2949,6 +2953,39 @@ def update_source_code_style(docx_input, docx_output): print(f'Changed style "SourceCode" to "PL" and added borders to {counter} paragraphs') # Inline code: apply Courier New + shading on the run (character formatting). # Note: w:rStyle must reference a character styleId from styles.xml — "Courier New" # is a font name, not a style, so set w:rFonts on w:rPr instead. Paragraph pStyle # does not block this; character formatting applies only to this run. for elem in root.xpath('.//w:rStyle[@w:val="Verbatim" or @w:val="VerbatimChar"]', namespaces=ns): rPr = elem.getparent() if rPr is None: continue r = rPr.getparent() if r is None: continue para = r.getparent() if para is None: continue # Remove the Verbatim/VerbatimChar character style reference rPr.remove(elem) # Set Courier New as the run font rFonts = rPr.xpath('./w:rFonts', namespaces=ns) if rFonts: rFonts_elem = rFonts[0] else: rFonts_elem = etree.Element(f"{{{ns['w']}}}rFonts") rPr.insert(0, rFonts_elem) for attr in ('ascii', 'hAnsi', 'cs', 'eastAsia'): rFonts_elem.set(f"{{{ns['w']}}}{attr}", "Courier New") add_shading_to_element(rPr) counterInlineCode += 1 print(f'Applied Courier New + shading to {counterInlineCode} Verbatim/VerbatimChar runs') xml_data = etree.tostring(root, xml_declaration=True, encoding="UTF-8", standalone="yes") # create temp file Loading Loading
generateBaseline/postprocessing.py +52 −15 Changes for generateBaseline/postprocessing.py: 52 added lines, 15 removed lines. Original line number Diff line number Diff line Loading @@ -2879,6 +2879,24 @@ def update_source_code_style(docx_input, docx_output): root = etree.fromstring(xml_data) new_style = "PL" counter = 0 counterInlineCode = 0 def add_shading_to_element(elem): """Helper function to add shading to an element""" if elem is None: return shd = elem.xpath('./w:shd', namespaces=ns) if not shd: shd_elem = etree.Element(f"{{{ns['w']}}}shd") shd_elem.set(f"{{{ns['w']}}}val", "clear") shd_elem.set(f"{{{ns['w']}}}color", "auto") shd_elem.set(f"{{{ns['w']}}}fill", "F2F2F2") elem.append(shd_elem) else: shd_elem = shd[0] shd_elem.set(f"{{{ns['w']}}}val", "clear") shd_elem.set(f"{{{ns['w']}}}color", "auto") shd_elem.set(f"{{{ns['w']}}}fill", "F2F2F2") def add_borders_to_paragraph(pPr): """Helper function to add borders and background color to a paragraph""" Loading Loading @@ -2915,21 +2933,6 @@ def update_source_code_style(docx_input, docx_output): for attr, value in border_attrs.items(): border_elem[0].set(attr, value) # Add grey background color (shading) shd = pPr.xpath('./w:shd', namespaces=ns) if not shd: shd_elem = etree.Element(f"{{{ns['w']}}}shd") shd_elem.set(f"{{{ns['w']}}}val", "clear") # Clear pattern shd_elem.set(f"{{{ns['w']}}}color", "auto") # Auto text color shd_elem.set(f"{{{ns['w']}}}fill", "F2F2F2") # Light grey background pPr.append(shd_elem) else: # Update existing shading shd_elem = shd[0] shd_elem.set(f"{{{ns['w']}}}val", "clear") shd_elem.set(f"{{{ns['w']}}}color", "auto") shd_elem.set(f"{{{ns['w']}}}fill", "F2F2F2") # Loop over all elements to find "SourceCode" and change to "PL", and add borders for elem in root.xpath('.//w:pStyle[@w:val="SourceCode"]', namespaces=ns): old_val = elem.get(f"{{{ns['w']}}}val") Loading @@ -2939,6 +2942,7 @@ def update_source_code_style(docx_input, docx_output): # Get the paragraph element to add borders pPr = elem.getparent() add_borders_to_paragraph(pPr) add_shading_to_element(pPr) # Remove other rPr:rStyle elements para = pPr.getparent() Loading @@ -2949,6 +2953,39 @@ def update_source_code_style(docx_input, docx_output): print(f'Changed style "SourceCode" to "PL" and added borders to {counter} paragraphs') # Inline code: apply Courier New + shading on the run (character formatting). # Note: w:rStyle must reference a character styleId from styles.xml — "Courier New" # is a font name, not a style, so set w:rFonts on w:rPr instead. Paragraph pStyle # does not block this; character formatting applies only to this run. for elem in root.xpath('.//w:rStyle[@w:val="Verbatim" or @w:val="VerbatimChar"]', namespaces=ns): rPr = elem.getparent() if rPr is None: continue r = rPr.getparent() if r is None: continue para = r.getparent() if para is None: continue # Remove the Verbatim/VerbatimChar character style reference rPr.remove(elem) # Set Courier New as the run font rFonts = rPr.xpath('./w:rFonts', namespaces=ns) if rFonts: rFonts_elem = rFonts[0] else: rFonts_elem = etree.Element(f"{{{ns['w']}}}rFonts") rPr.insert(0, rFonts_elem) for attr in ('ascii', 'hAnsi', 'cs', 'eastAsia'): rFonts_elem.set(f"{{{ns['w']}}}{attr}", "Courier New") add_shading_to_element(rPr) counterInlineCode += 1 print(f'Applied Courier New + shading to {counterInlineCode} Verbatim/VerbatimChar runs') xml_data = etree.tostring(root, xml_declaration=True, encoding="UTF-8", standalone="yes") # create temp file Loading