16

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.

3 Answers 3

20

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.

Sign up to request clarification or add additional context in comments.

1 Comment

In the Pandas excel writer, ws.sheet_view.showGridLines does not work. You should do worksheet.screen_gridlines = False
11

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()

6 Comments

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.
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.
+1 for XlsxWriter. @sherve: I've done some informal performance testing, and for me XlsxWriter is comparable to xlwt, and noticeably faster than openpyxl.
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.
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.
|
1

For removing major grid lines of your plot in openpyxl use:

mychart.x_axis.majorGridlines = None
mychart.y_axis.majorGridlines = None

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.