How to retrieve merged cell ColSpan property from table cell objects in Word? Currently, only Rowspan property is visible

I want to retrieve the situation of merging cells, and I feel that this is the most basic attribute

To retrieve the ColSpan (column span) property of a merged cell in LibreOffice Calc, you need to use the UNO API, which allows you to access and manipulate various table (spreadsheet) attributes, including merged cells. The ColSpan property isn’t directly available, but you can determine it through properties such as IsMerged or CellRangeAddress.

Here’s how you can retrieve this information using Python with PyUNO:

Example Python (PyUNO) Script for LibreOffice Calc:

from com.sun.star.uno import Exception as UnoException

def get_merged_cells(sheet):
    # Loop through rows and columns of a sheet
    for row in range(sheet.Rows.Count):
        for col in range(sheet.Columns.Count):
            try:
                # Get the cell object
                cell = sheet.getCellByPosition(col, row)
                
                # Get the cell range address (includes merged region)
                cell_range_address = cell.getRangeAddress()
                
                # Check if it's part of a merged cell
                if cell_range_address.EndColumn != cell_range_address.StartColumn:
                    col_span = cell_range_address.EndColumn - cell_range_address.StartColumn + 1
                else:
                    col_span = 1  # Not merged
                
                print(f"Cell[{row}, {col}] - ColSpan: {col_span}")
                
            except UnoException as e:
                print(f"Error: {e}")

Explanation:

  • The script accesses each cell in the specified sheet.
  • getRangeAddress() provides the merged cell’s range, which allows you to calculate the ColSpan (difference between the EndColumn and StartColumn).
  • If the start and end columns differ, it means the cell spans across multiple columns.

https://api.libreoffice.org/
https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1table_1_1XCellRange.html
https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1table_1_1XCell.html

Some forum ans that may also help

@darshan Thank you for your reply. I am in the writer, not calc. I have searched for Cell, CellRange, and Table objects, but have not found any properties similar to ColumnSpan

To retrieve the ColSpan (column span) property from a table cell inside LibreOffice Writer using Python and UNO, you can access the table’s structure through the API. The table cells don’t expose a direct ColSpan property like you might expect, but you can still calculate the column span by checking whether a cell is part of a merged range.


from com.sun.star.uno import Exception as UnoException

def get_colspan_from_table(writer_table):
    rows = writer_table.Rows.Count
    cols = writer_table.Columns.Count

    for row_idx in range(rows):
        for col_idx in range(cols):
            try:
                # Access the cell at (col_idx, row_idx)
                cell = writer_table.getCellByPosition(col_idx, row_idx)

                # Get the cell's range address
                cell_range = cell.getRangeAddress()

                # Calculate ColSpan based on the range of merged columns
                col_span = cell_range.EndColumn - cell_range.StartColumn + 1

                # Print the ColSpan of the current cell
                print(f"Cell[{row_idx}, {col_idx}] - ColSpan: {col_span}")

            except UnoException as e:
                print(f"Error: {e}")

# Assuming 'doc' is your opened LibreOffice Writer document
# Access the table (replace '0' with the correct table index if needed)
writer_table = doc.TextTables.getByIndex(0)
get_colspan_from_table(writer_table)
  • Get Table Object: getCellByPosition(col_idx, row_idx) is used to access each cell in the table.
  • Cell Range: getRangeAddress() retrieves the start and end columns of the cell’s range.
  • Calculate ColSpan: By subtracting the StartColumn from EndColumn, you can determine how many columns the cell spans.

https://api.libreoffice.org/docs/idl/ref/XTextTable_8idl.html

Thanks,
Darshan

@darshan Still not working, it just reported an error. The cell object does not have the getRangeAddress() function

Oh, I see, you can check the cell’s IsMerged property and retrieve the range of merged cells using the getRangeAddresses() method from the Table object.
Sudo code:



                # Initialize ColSpan
                col_span = 1

                # Check if the cell is part of a merged cell
                if cell.IsMerged:
                    # If merged, find the range address of the merged cell
                    merged_range = cell.getRangeAddresses()[0]
                    col_span = merged_range.EndColumn - merged_range.StartColumn + 1

Perhaps it’s useful to know that in Writer’s table model, there is no such thing as “col span”. The model is just a list of rows, and then each row has a list of cells. Any definition of columns is just UI/UNO layer on top of this model. That may explain why it’s hard to find any explicit info about columns.

1 Like

@vmiklos It is very likely that this is a very basic attribute, and it feels like a design flaw. The presentation and Calc do not have this issue.

@darshan I have tried everything that still doesn’t work because the cell object doesn’t have the IsMerged and getRangeAddresses properties

@gulixiang nah, it’s simply the Writer table model (table is a list of rows, row is a list of cells) fits the ODT and DOCX table model. I understand it’s not a good fit for the HTML table model, and col span is a concept in the HTML table model only.

Are you interested in contributing so the UNO API would hide this complexity?

Code pointer: you can already get column separators for a table (on a 0…10000 scale) here:

You could implement something similar, to determine what are the UI-level columns of a table and then determine how many of those columns cover your table box.

table-demo.docx (4.6 KB)
Your guess is correct. The table in the document does not have the concept of col span. I guess he wants to achieve more flexible formatting. Look at the document I uploaded, there is a table inside the document, and in this case, col span cannot be calculated.