Removing gridlines from excel using python (openpyxl)
13,560
Solution 1
There is a relevant issue in openpyxl
issue tracker. Plus, according to the source code show_gridlines
is just a worksheet class property that has no affect at all. Just watch the issue to get any update on it.
As an alternative solution, try the new and awesome xlsxwriter module. It has an ability to hide grid lines on a worksheet (see docs). Here's an example:
from xlsxwriter.workbook import Workbook
workbook = Workbook('hello_world.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello world')
worksheet.hide_gridlines(2)
workbook.close()
Solution 2
This was fixed in 2015.
Here is the recommended solution (from description of issue)
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.sheet_view.showGridLines
True
ws.sheet_view.showGridLines = False
wb.save("gridlines.xlsx")
Beware that you should type ws.sheet_view.showGridLines
and not .ws.showGridLines
Author by
Admin
Updated on June 09, 2022Comments
-
Admin almost 2 years
I'm trying to remove gridlines from excel worksheet which I created using openpyxl, and it's not working. I'm doing this:
wb = Workbook() ws = wb.get_active_sheet() ws.show_gridlines = False print ws.show_gridlines wb.save('file.xlsx')
The that code prints the 'False', yet the saved file shows gridlines.
-
sherve about 11 yearsxlsxwriter does look good. Thanks for the link. did you profile it? How does it compare to xlwt? I've used openpyxl in the past and it was way slower than xlwt if you are writing lots of cells.
-
alecxe about 11 yearsYup, looks promising! Unfortunately, I haven't profiled it. FYI, here's some info on performance subject. Plus, obviously, it can work with
xlsx
only. -
John Y almost 11 years+1 for XlsxWriter. @sherve: I've done some informal performance testing, and for me XlsxWriter is comparable to xlwt, and noticeably faster than openpyxl.
-
sherve almost 11 yearsYes, I ended up doing my own performance measures and got the same results as you John. And even without the performance optimizations, it's still pretty close to xlwt. I've ported my scripts to XlsxWriter and so far pretty happy with it.
-
alecxe almost 11 yearsAnd, we've decided not to switch from
xlwt
toxlsxwriter
because for us report generation became ~1.5 slower on average. Hopexlsxwriter
will become faster in the future releases. -
Vyachez about 5 yearsI just switched to openpyxl from xlsxwriter and felt deep relief... last one is very awkward slow and illogical. openpyxl does its job smoothly and easy to code. Another huge issue with xlsxwriter - it is not able to read content.
-
Eelco van Vliet almost 2 yearsIn the Pandas excel writer, ws.sheet_view.showGridLines does not work. You should do worksheet.screen_gridlines = False