Horizontally Merged Cells

I am trying to pull table structures out of documents using words-python (version 25.9.0). I need to be able to detect merged cells properly. The vertical merges seem to work as anticipated based on documentation. However, I am encountering a problem with horizontal merges. I’ve attached an example file that I was trying to process. If I pull out the first cell in that table, it should have cell.cell_format.horizontal_merge == aw.tables.CellMerge.FIRST, but it is instead aw.tables.CellMerge.NONE. I was able to detect horizontal merges by instead looking at the cell text as HTML and pulling out the colspan attribute from the td tag. However, this doesn’t seem like the right way to do it, so I wanted to check here if I’m missing something. Here’s some code copied out of my interpreter that you can use with the attached document to reproduce the issue:

doc = aw.Document('Merged Table Example.docx')
section = doc.sections[0]
table = [child for child in section.body.get_child_nodes(aw.NodeType.TABLE, False)][0].as_table()
row = [child for child in table.get_child_nodes(aw.NodeType.ROW, False)][0].as_row()
cell = [child for child in row.get_child_nodes(aw.NodeType.CELL, False)][0].as_cell()
cell.to_string(save_format=aw.SaveFormat.TEXT)

'2 horizontal cells\r'

cell.cell_format.horizontal_merge

<CellMerge.NONE: 0>

cell.to_string(save_format=aw.SaveFormat.HTML)

'\<td colspan="2" style="width:222.9pt; border:0.75pt solid #000000; padding-right:5.03pt; padding-left:5.03pt; vertical-align:top">\<p style="margin-top:0pt; margin-bottom:0pt; font-size:12pt">\<span style="font-family:Aptos">2 horizontal cells\</span>\</p>\</td>'

Merged Table Example.docx (13.5 KB)

@seanwhitsitt

It appears you are experiencing an issue with detecting horizontally merged cells in a table using Aspose.Words for Python. The cell.cell_format.horizontal_merge property is intended to indicate the merge status, but if it is returning CellMerge.NONE for your merged cells, there may be an issue with how the document is structured or interpreted.

Here are some steps to troubleshoot the issue:

  1. Document Formatting: Ensure that the document is correctly formatted and saved in a way that Aspose.Words can recognize the merged cells. If the document is in a plain text format, consider converting it to a supported format like DOCX or HTML for better handling of table structures.

  2. Latest Version: Make sure you are using the latest version of Aspose.Words for Python, as updates may include important bug fixes related to table handling.

  3. Verify Table Structure: Check the original document to confirm that the cells are indeed merged. You can do this by inspecting the document in a word processor that supports table editing.

  4. Test with Different Documents: Try using a different document to determine if the issue is specific to the file you are currently working with.

If the problem persists after these checks, consider reaching out to the Aspose support forums for more specialized assistance.

Perhaps my version of Word is making ill-formed documents, but every document I’ve tried making has the same issue.

@seanwhitsitt In MS Word tables, there is no concept of “columns”. Each row in a table is independent and can contain any number of cells. Please refer to our documentation for more information:
https://docs.aspose.com/words/python-net/working-with-columns-and-rows/#work-with-columns

Therefore, horizontally merged cells in MS Word tables can be simulated by simple wide cells. You can use the Table.convert_to_horizontally_merged_cells method to convert “simple wide” cells to horizontally merged cells. After calling this method cell.cell_format.horizontal_merge will return the expected value.

@alexey.noskov, I did notice that the rows contained the “proper” amount of cells for the merges that were done. I like the approach to just including the cells that way rather than having to count the merged cells. However, do the “simple wide” cells provide an indication of how wide they are (I tried to look for “wide” in that link and the term doesn’t appear in the page)? That is, is there another way to look up the colspan value? I think my method of getting the HTML for the cell and checking the colspan attribute is preferable to counting cells, but if there’s a more efficient way to check that value (e.g., without converting to HTML and then parsing) then I’d prefer that.

Side note, I don’t actually need to look at the cells in columns. I specifically need to understand how cells are merged in a table. I can see how that would lead to investigating rows versus columns with vertical/horizontal merges, but it’s fairly moot to what I need. Also, I don’t actually see “Table.convert_to_horizontally_merged_cells” mentioned in the documentation link you provided. I was able to look it up separately though. It’s possible I’m blind or my find-in-page is leading me astray, but I also don’t see discussion of merged cells in that link (outside of a separate link in the sidebar that leads to a page that discusses looking at the CellFormat.HorizontalMerge property).

@seanwhitsitt

You can call Table.auto_fit method with AutoFitBehavior.FIXED_COLUMN_WIDTHS to force Aspose.Words recalculate table cell width and specify the calculated cell width in CellFormat.preferred_width property.

If I understand you requirements properly, your goal is to determine horizontally merged cells. To achieve this, the most efficient way is to call Table.convert_to_horizontally_merged_cells method, then loop through cells in each row and use cell.cell_format.horizontal_merge property.