'Openpyxl - 'MergedCell' object attribute 'value' is read-only

If I do it the commented way, it works great.

But to clean up the code a little bit, I made a function to do it.

But if I try do it the function way,

I get Openpyxl - 'MergedCell' object attribute 'value' is read-only:

"Traceback (most recent call last):
  File "c:/Users/vitor.augusto/Desktop/projeto_robozinho/robot_api/excel.py", line 67, in <module>
    preenche_linha(nr_solicitacao, 'D', start+1, 'center', 'total', ws)
  File "c:\Users\vitor.augusto\Desktop\projeto_robozinho\robot_api\functions\utils.py", line 31, in preenche_linha
    ws[f'{coluna}{linha}'] = valor
AttributeError: 'MergedCell' object attribute 'value' is read-only"

enter image description here



Solution 1:[1]

I'm not going to compare the two code snippets just postulate on what the error could be.
It would seem likely that the cell referenced by ws[f'{column}{row}'] is part of a merged cell set but is not the top left cell so cannot be written to.
E.g. I merge cells A1, B1, and C1 in a sheet. Cell A1 is the top left cell and is now that merged cell coordinate, B1 and C1 effectively no longer exist. If I attempted to enter a value into either B1 or C1

column = 2
row = 1
ws[f'{column}{row}'] = value

it would result in the AttributeError: 'MergedCell' object attribute 'value' is read-only error.
Check your code values for column and row being passed to the function when the error occurs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1