python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.38k stars 1.08k forks source link

The cell's vMerge attribute may be incorrect #1380

Closed iclgg closed 2 months ago

iclgg commented 2 months ago

When I attempt to process the table through the vMerge attribute of cells, I've noticed that the vMerge value for some cells should be 'continue', yet it actually appears as 'restart'. When I use the code below, I noticed that the vMerge attribute value obtained for the same cell is different in two ways. Is this due to some design intention or is it an error?

    for table in doc.tables:
        for row in table.rows:
            tr = row._tr
            row_cell_tc = [tc for tc in tr.tc_lst]
            cell_tc = [cell._tc for cell in row.cells]
scanny commented 2 months ago

This has to do with the way row.cells works.

Check out this page in the docs and then if you still have questions let me know: https://python-docx.readthedocs.io/en/latest/user/tables.html

Basically, [tc for tc in tr.tc_lst] with give you the actual value in the w:tc element. cell in row.cells gives you the "root" (top-left) w:tc element for all spanned cells in the merge. So vMerge for that will always be "restart" (when it exists).

Feel free to reopen if you end up with more questions after reading.