You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to detect merged cells when reading tables #232
Hello, I've created a simple .docx document (Word 2010) with a simple 2x2 table, the 1st col is merged as with:
+---+---+
| | b |
+ a +---+
| | c |
+---+---+
When reading this with python the cells (0,0) and (1,0) are different!
How can I detect that they are merged on the original document?
The text of both cell is indeed 'a'. But the 'pointer' are different.
Thanks
Python 2.7, docx 0.85
I've found a workaround (though the doc does not stand).
With the above table in simple.docx
from docx import Document
doc=Document("simple.docx")
table=doc.tables[0]
c00=table.cell(0,0)
c10=table.cell(1,0)
c00==c01 # is false as reported
#however
c00._tc==c01._tc # is true
I found that some docx files created by word have inconsistend grid_span attributes in a row.
When accessing a specific cell the ._cells property will not care about the rows defined by the xml but just count through the cells and assume that the cell count per row and all grid_span are correct.
In my case there were 7 columns in the table but one row contained only grid_span attribues of 1 + 1 + 2 + 2 = 6. After this row, all the succeeding rows are broken.
The workaround for detecting this has already been mentioned by @oltish.
I will provide a merge request as a fix for the _cells function.
Activity
sylvain-bougnoux commentedon Dec 1, 2015
I've found a workaround (though the doc does not stand).
With the above table in simple.docx
Regards
oltish commentedon May 6, 2017
It has been a while, but someone else may need this information in future.
As mentioned in previous comment, docx.table._Cell object has property _tc which contains useful information about cell's span:
The output will be:
From this you will easily understand not only if the cell is merged or not, but also its shape and size.
0-173 commentedon Oct 28, 2018
I found that some docx files created by word have inconsistend grid_span attributes in a row.
When accessing a specific cell the ._cells property will not care about the rows defined by the xml but just count through the cells and assume that the cell count per row and all grid_span are correct.
In my case there were 7 columns in the table but one row contained only grid_span attribues of 1 + 1 + 2 + 2 = 6. After this row, all the succeeding rows are broken.
The workaround for detecting this has already been mentioned by @oltish.
I will provide a merge request as a fix for the _cells function.
mrlnc commentedon Jul 30, 2019
The workaround from @0-173 works for me. Thanks! However, the new cell doesn't have any attributes like the "text" field.
python2
docx 0.8.10
fusted2 commentedon Aug 28, 2021
Thank you for your idea. I create a list, and add cell_.tc, then check to skip the 2nd+ merged cells. This is my project: