Commit 3bb928cf authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Fix for paragraphs in tables with no pPr element

parent 3713bd0f
Loading
Loading
Loading
Loading
+30 −10
Changes for generateBaseline/postprocessing.py: 30 added lines, 10 removed lines.
Original line number Original line Diff line number Diff line
@@ -1949,18 +1949,37 @@ def update_table_rows(docx_input, docx_output):
    counter_tal = 0
    counter_tal = 0
    counter_tar = 0
    counter_tar = 0


    # Ensure table paragraphs without pPr are treated like Compact paragraphs.
    for para in root.xpath('.//w:tr//w:p[not(w:pPr)]', namespaces=ns):
        pPr = OxmlElement('w:pPr')
        pStyle = OxmlElement('w:pStyle')
        pStyle.set(f"{{{ns['w']}}}val", "Compact")
        pPr.append(pStyle)
        para.insert(0, pPr)

    # Find all Compact style elements that are within a w:tr (table row)
    # Find all Compact style elements that are within a w:tr (table row)
    for elem in root.xpath('.//w:tr//w:pStyle[@w:val="Compact"]', namespaces=ns):
    for pPr in root.xpath('.//w:tr//w:pPr', namespaces=ns):
        # Check if the w:p/w:pPr element has a pStyle, if not, add it
        pStyle_elems = pPr.xpath('./w:pStyle', namespaces=ns)
        if not pStyle_elems:
            pStyle_elem = etree.Element(f"{{{ns['w']}}}pStyle")
            pStyle_elem.set(f"{{{ns['w']}}}val", "Compact")
            pPr.insert(0, pStyle_elem)
        elif pStyle_elems[0].get(f"{{{ns['w']}}}val") != "Compact":
            continue
        else:
            pStyle_elem = pStyle_elems[0]
        
        # Find the parent w:tr element
        # Find the parent w:tr element
        tr = elem
        tr = pPr
        while tr is not None and tr.tag != f"{{{ns['w']}}}tr":
        while tr is not None and tr.tag != f"{{{ns['w']}}}tr":
            tr = tr.getparent()
            tr = tr.getparent()
        
        
        if tr is None:
        if tr is None:
            continue
            continue
        
        
        # Find the paragraph element that contains this pStyle
        # Find the paragraph element that contains this pPr
        para = elem
        para = pPr
        while para is not None and para.tag != f"{{{ns['w']}}}p":
        while para is not None and para.tag != f"{{{ns['w']}}}p":
            para = para.getparent()
            para = para.getparent()
        
        
@@ -1970,9 +1989,10 @@ def update_table_rows(docx_input, docx_output):
        # Check if this row has tblHeader with value "on"
        # Check if this row has tblHeader with value "on"
        tbl_header = tr.xpath('./w:trPr/w:tblHeader[@w:val="on"]', namespaces=ns)
        tbl_header = tr.xpath('./w:trPr/w:tblHeader[@w:val="on"]', namespaces=ns)
        
        
        
        if tbl_header:
        if tbl_header:
            # It's a header row, change to TAH
            # It's a header row, change to TAH
            elem.set(f"{{{ns['w']}}}val", "TAH")
            pStyle_elem.set(f"{{{ns['w']}}}val", "TAH")
            counter_tah += 1
            counter_tah += 1
            # Remove ALL properties from pPr except pStyle
            # Remove ALL properties from pPr except pStyle
            # Header alignment is implicit in TAH style, so explicit jc must be removed
            # Header alignment is implicit in TAH style, so explicit jc must be removed
@@ -2018,20 +2038,20 @@ def update_table_rows(docx_input, docx_output):
            jc_elem = para.xpath('./w:pPr/w:jc', namespaces=ns)
            jc_elem = para.xpath('./w:pPr/w:jc', namespaces=ns)
            if not jc_elem:
            if not jc_elem:
                # No jc present, default to TAC
                # No jc present, default to TAC
                elem.set(f"{{{ns['w']}}}val", "TAC")
                pStyle_elem.set(f"{{{ns['w']}}}val", "TAC")
                counter_tac += 1
                counter_tac += 1
            else:
            else:
                # jc exists, check the value
                # jc exists, check the value
                jc_val = jc_elem[0].get(f"{{{ns['w']}}}val")
                jc_val = jc_elem[0].get(f"{{{ns['w']}}}val")
                if jc_val == "left":
                if jc_val == "left":
                    elem.set(f"{{{ns['w']}}}val", "TAL")
                    pStyle_elem.set(f"{{{ns['w']}}}val", "TAL")
                    counter_tal += 1
                    counter_tal += 1
                elif jc_val == "right":
                elif jc_val == "right":
                    elem.set(f"{{{ns['w']}}}val", "TAR")
                    pStyle_elem.set(f"{{{ns['w']}}}val", "TAR")
                    counter_tar += 1
                    counter_tar += 1
                else:
                else:
                    # center or other, default to TAC
                    # center or other, default to TAC
                    elem.set(f"{{{ns['w']}}}val", "TAC")
                    pStyle_elem.set(f"{{{ns['w']}}}val", "TAC")
                    counter_tac += 1
                    counter_tac += 1
        
        
    print(f'Changed style "Compact/TableHeading" to "TAH" {counter_tah} times, "Compact/TableRow" to "TAL" {counter_tal} times, to "TAR" {counter_tar} times, to "TAC" {counter_tac} times')
    print(f'Changed style "Compact/TableHeading" to "TAH" {counter_tah} times, "Compact/TableRow" to "TAL" {counter_tal} times, to "TAR" {counter_tar} times, to "TAC" {counter_tac} times')
@@ -3243,10 +3263,10 @@ def update_format_styles_cli():
        ("update_figure_style", update_figure_style),
        ("update_figure_style", update_figure_style),
        ("update_table_captions", update_table_captions),
        ("update_table_captions", update_table_captions),
        ("update_abbreviations", update_abbreviations),
        ("update_abbreviations", update_abbreviations),
        ("update_lists", update_lists),
        ("update_table_rows", update_table_rows),
        ("update_table_rows", update_table_rows),
        ("update_notes", update_notes),
        ("update_notes", update_notes),
        ("update_references", update_references),
        ("update_references", update_references),
        ("update_lists", update_lists),
        ("repair_invalid_numbering_references", repair_invalid_numbering_references),
        ("repair_invalid_numbering_references", repair_invalid_numbering_references),
        ("update_body_text_style", update_body_text_style),
        ("update_body_text_style", update_body_text_style),
        ("add_no_break_hyphens", add_no_break_hyphens),
        ("add_no_break_hyphens", add_no_break_hyphens),