Normally Excel remembers where you stopped at a sheet and when you open it next time it takes you right there. Is there a way to set up such a position when generating the document?
Advertisement
Answer
With the pywin32 package, you can control Excel with COM and automate anything that Excel can do.
Here’s an example that creates a simple worksheet, initializes some cells, selects a cell, then saves the document. When you later open the document it will restore to that same location.
JavaScript
x
17
17
1
import win32com.client
2
3
def create_excel_file():
4
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
5
# xl.Visible = True # Uncomment to make the Excel COM instance visible
6
wb = xl.Workbooks.Add()
7
ws = xl.ActiveSheet
8
ws.Range('A1').Value = 100
9
ws.Range('A2').Value = 200
10
ws.Range('B1').Value = 300
11
ws.Range('B2').Value = 400
12
ws.Range('B2').Select()
13
ws.SaveAs(r'test.xlsx')
14
xl.Quit()
15
16
create_excel_file()
17
Here’s the resulting file opened in Excel: