1

I want to open an excel file, let it wait for a bit (there is a code that needs to run and returns some data) and then save it to another csv file. I tried to open the excel file first using the pywin32 package but it does not work. The code ran without any issue but Excel just does not open. Here is my code:

import win32com.client as win32

xl = win32.Dispatch('Excel.Application')
wb = xl.Workbooks.Open('C:/Users/123456/OneDrive/PyCharm/my-stdout.csv')

How to get this to open excel, and how to make it wait a bit and then save as another csv file?

1 Answer 1

0

In Python, waiting is usually accomplished with the use of the sleep function (waiting time in seconds):

import time
do_something()
time.sleep(5)
do_something_else()

For opening up Excel, you should use:

excel = win32.gencache.EnsureDispatch('Excel.Application')

And finally, make sure you use the full path as you are and not just the filename and display the application using:

excel.Visible = True
Sign up to request clarification or add additional context in comments.

1 Comment

it opens excel now, but it opens many files. I used the following codes: xl = win32.gencache.EnsureDispatch('Excel.Application') wb = xl.Workbooks.Open('C:/Users/123456/Documents/test.csv') xl.Visible = True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.