question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to detect merged cells when reading tables

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

5reactions
oltishcommented, 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:

from docx import Document
doc=Document("simple.docx")
table=doc.tables[0]

c=table.cell(0,0)
print(c.text, c._tc.top, c._tc.bottom, c._tc.left, c._tc.right)

c=table.cell(0,1)
print(c.text, c._tc.top, c._tc.bottom, c._tc.left, c._tc.right)

c=table.cell(1,1)
print(c.text, c._tc.top, c._tc.bottom, c._tc.left, c._tc.right)

The output will be:

a 0 2 0 1
b 0 1 1 2
c 1 2 1 2

From this you will easily understand not only if the cell is merged or not, but also its shape and size.

2reactions
sylvain-bougnouxcommented, Dec 1, 2015

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

Regards

Read more comments on GitHub >

github_iconTop Results From Across the Web

Detecting merged cells in a word document table
The only way to detect them will be to analyse all the tables with the algorithm you found in the posts linked in...
Read more >
Identify the merged cells while reading table : Spire.Doc
Hi Team, We need to identify the merged cells while reading/extracting the content of from the table inside word file.
Read more >
How to read Merged Cells from Excel in Power BI ... - YouTube
Learn more about Power Query:https://radacad.com/category/power-query.
Read more >
Merge and combine cells in Excel without losing data - Ablebits
None of standard Excel merging options works for the cells inside an Excel table. You have to convert a table to a usual...
Read more >
How to recognize merged cells in a table? help!!!!!
Horizontally merged cell also can detect, but it will need some more coding. But, unfortunately, not by calling any WORD VBA method or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found