M
M
mezigar2021-07-07 10:19:12
Python
mezigar, 2021-07-07 10:19:12

How do you know if a cell is merged?

You need to go through the table in certain columns and read the data, but some cells may be merged, so they need to be torn apart and read / overwrite the data.

for cell in column_shablon_cells:
    #^^merged_range^^
    if (cell in sheet.merged_cells.ranges): # узкое горлышко
        unmerge_cells(cell)
    print(cell.col_idx)

Swears on a line with the condition:
TypeError: class 'openpyxl.cell.cell.Cell'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Akina, 2021-07-07
@mezigar

There are many ways to determine if a cell is merged.
For example, cell.CurrentRegion.Count returns the number of individual cells of the cell object - accordingly, for a merged cell, it will return a value greater than one.
If the cell is merged, then the opposite corner can be obtained like this: cell.Offset(1,1).Offset(-1,-1).Address . Knowing the cell nucleus and the address of the opposite corner, it is easy to construct an address for the reverse union by ordinary concatenation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question