Currently I'm working on generating a calender in Python using XlsxWriter. I've created an Excel document with 52 sheets (1 for every week in a year). Generating the entire document works perfectly fine but when opening the document, it always opens on the first sheet (week 1). I'm trying to set the opening sheet to the current week. Currently I'm generating my document as follows:
weeks = 52 # amount of weeks in a year
Excel_sheets = [] # empty list for creating new sheets
path = r"C:/Users/menno/Documents/calender.xlsx" # location for the excel file
Excel_doc = xlsxwriter.Workbook(path) # create file at given path
for week in range(weeks): # loopt through amount of weeks
Excel_sheets.append(Excel_doc.add_worksheet("Week " + str(week + 1))) # for every week make a new sheet
So now I've generated an Excel file with 52 sheets, each one named "week 1" until "week 52". I then put data in each sheet and finally I close it. When I open the document it will always open on the first sheet. Is there any way I can make it so it automatically opens on a different sheet, or more specifically, open the current week that the file was generated in?
I looked through the XslxWriter documentation but I couldn't find a function that allows me to set a starting sheet. If anyone has any idea then please let me know. Thanks in advance!
With XlsxWriter you can can set the visible worksheet with the worksheet activate() method.
In some versions of Excel the activated worksheet may not be on the screen (although with most recent versions it will be) and you may also have to set the first visible worksheet using set_first_page().
Something like this:
import xlsxwriter
weeks = 52
Excel_sheets = []
path = r"calender.xlsx"
Excel_doc = xlsxwriter.Workbook(path)
for week in range(weeks):
Excel_sheets.append(Excel_doc.add_worksheet("Week " + str(week + 1)))
Excel_sheets[19].set_first_sheet()
Excel_sheets[20].activate()
Excel_doc.close()
Output: