Jame Stitel
Jame Stitel

Reputation: 15

Openpyxl finds merged cells even though there are none

I'm handling excels in my python script and get the following error:

    Traceback (most recent call last):
  File "script_consolidateExcels_v3.py", line 134, in <module>
    colNum = findColOfCell(ws, "xxx", 1)
  File "script_consolidateExcels_v3.py", line 28, in findColOfCell
    if worksheet.cell(row=inRow, column=k).value == val:
AttributeError: 'MergedCell' object has no attribute 'value'

The excel file initially had merged cells, but I unmerged everything using Microsoft Excel, yet this error still pops up. I even tried ws.unmerge_cells(), but with no help. (Weirdly enough, at first I wrote and run the script on a windows laptop and this error did not pop up... I sent the script to my mac, and now I get this error... (I can't access the windows laptop currently))

Here is the problematic part:

def findColOfCell(worksheet, val, inRow, startCol=1):
    i = 0
    k = startCol
    while i == 0:
        if worksheet.cell(row=inRow, column=k).value == val:
            i = 1
            # here = ord(worksheet.cell(row=inRow, column=k).column)-64 # convert column letter to number
            here = worksheet.cell(row=inRow, column=k).column
        else: k += 1
    return here

Upvotes: 0

Views: 1403

Answers (1)

Vishal Sharma
Vishal Sharma

Reputation: 31

If you are merging 3 cells say (1,1) (1,2) and (1,3)

sheet.merge_cells(start_row=1, start_column=1, end_row=1, end_column=3)

In this case, cell (1,2) and (1,3) will no longer have attribute 'value'.

You can only assign value to (1,1) as the cell (1,2) and (1,3) does not exist anymore.

Hope this helps.

Upvotes: 1

Related Questions