Home » excel » python – How can I set the "default" worksheet with xlwt?

python – How can I set the "default" worksheet with xlwt?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m using Python and xlwt to create Excel files:

wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('Sheet1')
# ... fill Sheet1 with some data ...
ws = wb.add_sheet('Sheet2')
# ... fill Sheet2 with some data ...
wb.save('test.xls')

When I open the created file I see Sheet1. Would it be possible to set the “default” worksheet to something else with xlwt? For example, I would like to see Sheet2 when I open the file. I don’t want to re-order the sheets

How to&Answers:

workbook.active_sheet is a sheet index and defaults to 0. In your specific case above, you would do wb.active_sheet = 1.

Note: Currently, this forces the index of the first visible tab in the worksheet tab bar to be the same number. If you really don’t like that idea, join the python-excel google-group and make a case for it being settable independently.