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.
import win32com.client def create_excel_file(): xl = win32com.client.gencache.EnsureDispatch('Excel.Application') # xl.Visible = True # Uncomment to make the Excel COM instance visible wb = xl.Workbooks.Add() ws = xl.ActiveSheet ws.Range('A1').Value = 100 ws.Range('A2').Value = 200 ws.Range('B1').Value = 300 ws.Range('B2').Value = 400 ws.Range('B2').Select() ws.SaveAs(r'test.xlsx') xl.Quit() create_excel_file()
Here’s the resulting file opened in Excel: