I have two sheets in an Excel file and the first one is a cover sheet which I don't need to edit. There are a few merged cells in the cover sheet, and when I edit the file using openpyxl, without even touching the cover sheet, I lose borders from the merged cells. I am using load_workbook('excel file')
to load the Excel file and saving it with a different filename.
Is there any way to fix this problem?
For me this code work perfectly:
I simply find all merged cells and apply style of left top cell to the rest of the cells.
I know this is old but I got the same issue and the patch didn't work for me so I found a work around by using the function in the documentation that adds a style for merged cells and then looping on all the merged cells and calling the function on each range
Simple method
Go to the folder C:\Python\Lib\site-packages\ openpyxl\worksheet folder\
Open worksheet.py file
In merge_cells function comment the following lines
Save it.
That's all , it works for me.
Actual solution is to patch the libraries code by including this snippet after including the library, it fixes the problem. (Note: don't worry about missing definitions, e.g. COORD_RE, i.e. the patch is self-contained)
Source https://bitbucket.org/openpyxl/openpyxl/issues/365/styling-merged-cells-isnt-working