'Python open and save Excel file. Do I need to state sleep time?
My Excel file when opened will need 30 seconds to load data from a database. My script below successfully Open, Wait till data fully loaded then Save and Close Excel file one by one.
So my question is: Does python by default must wait till the workbook is fully load before it move to the next command Save(). And then wait until Save() is completed to execute Close(). I don't mind to put time.sleep(10) but I want to know how python processes the command sequence in this case.
(When I do parsing data from web, I need to command Python to wait for a certain element is loaded. That's why I wonder if I need to instruct Python to do the same with Excel.)
files = ['file1','file2']
path = f'C:\\Users\\Myfolder\\'
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True
for i in files:
print(i)
wb = excel.Workbooks.Open( path + str(i)+'.xlsx')
# time.sleep(25)
for wb_name in excel.Workbooks: #list all currently open excel file
if wb_name.Name == str(i)+'.xlsx' : #Save exact file name
print("Save workbook:",wb_name.Name)
wb_name.Save()
wb_name.Close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
