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.

Share:
13,560
Admin
Author by

Admin

Updated on June 09, 2022

Comments

  • Admin
    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
    sherve about 11 years
    xlsxwriter 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
    alecxe about 11 years
    Yup, 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
    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
    sherve almost 11 years
    Yes, 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
    alecxe almost 11 years
    And, we've decided not to switch from xlwt to xlsxwriter because for us report generation became ~1.5 slower on average. Hope xlsxwriter will become faster in the future releases.
  • Vyachez
    Vyachez about 5 years
    I 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
    Eelco van Vliet almost 2 years
    In the Pandas excel writer, ws.sheet_view.showGridLines does not work. You should do worksheet.screen_gridlines = False