1
votes

I am opening a .xlsm file using openpyxl abd updated some cells in it and then saved as .xlsm file.Now when I open the saved file I see that the cells which were merged in the original file are broken in new file.

the code which i am using is-

from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('Excel.xlsm',read_only=False ,keep_vba=True)
ws = wb['K0 Reg Patch Util']
ws.cell(row=42,column=3).value = 25
ws.cell(row=43,column=3).value = 30      
ws.cell(row=44,column=3).value = 24
wb.save('Test.xlsm')

Even on simple opening and saving file with openpyxl merged columns borders in the original file are broken. I have searched many times regarding this problem but none of the solutions were satisfying. I even came across a monkeypatch script which is to be included in the script after including the openpyxl library.The source for the script is-

https://bitbucket.org/openpyxl/openpyxl/issues/365/styling-merged-cells-isnt-working

The monkeypatch will overwrite the definition of merged cell from that present in the library.

can somebody tell me how to include this patch in the script and What does "self" means in the script.

1

1 Answers

1
votes

I ran into a similar issue. But for me this only occurs with "protected" excel files. Removing that protection worked for me. No more "broken" merged cells. At least for files with .XLSX extension, I did not test .XLSM.