0

Ifollowed this link to check how to open an excel file (open in a way similar to double cliking the file). How to open an Excel file with Python to display its content?

wnnmaw's answer works great but then i tried to improve it by using Stephan's answer since even wnnmaw says that Stephan's way is the way to go for only one file. Hower for some reasons, for me, the with part does not handle the closing of the workbook.my program continues after the end of the while but the file remains opened. Here is the most basic version of the code, but even this does not work.

with subprocess.Popen(["start", "/WAIT", retFle], shell=True) as doc:
    #doc.poll()
    bbg_df.to_excel(retFle, sheet_name ='Feuil1', )

sys.exit()

thanks for you help. I actually have to use excel that way because I am entering some forulas that will activate an excel add in that will pull some data. So I am waiting for this data, retrieve them and then I want to close the workbook.

EDIT:

Thanks to @Aran-Fey comments I arrived to this but it does not do the trick yet.

doc = subprocess.Popen(["start", "/WAIT", retFle], shell = True) 
bbg_df.to_excel(retFle, sheet_name ='Feuil1', )
time.sleep(10)
for x in psutil.Process(doc.pid).children(recursive = True):
    x.kill()
psutil.Process(doc.pid).kill()
A.david
  • 37
  • 10
  • Is there a way to tag the poster of the original answer I am referring? I am not sure how to do this? – A.david Apr 27 '18 at 12:37
  • Post a link at the end of your question. – What Apr 27 '18 at 12:38
  • Which file are you talking about? The excel file? If so, the statement "close file after Popen" doesn't make any sense. After the process has finished, the file is already closed. Are you trying to ask how to terminate a subprocess? – Aran-Fey Apr 27 '18 at 12:43
  • @Aran-Fey Yes sorry I meant terminate the subprocess. Sorry I thought it was clear. Based on the linked answer it seems the with should take care of closing the sub process but for me it does not – A.david Apr 27 '18 at 12:54
  • The context manager doesn't actively terminate the process; it simply waits for the process to finish. If you want to kill it, you have to do it manually. – Aran-Fey Apr 27 '18 at 13:01
  • @Aran-Fey I just tried doc.termina() and donc.kill() as the accepted answer in the question you linked buth this does nothing. Please see my edited question for the last version of the code – A.david Apr 27 '18 at 13:06
  • Whoops, my bad. I didn't realize processes started with `shell=True` needed special treatment. I added another dupe. (There's an answer that works on Windows if you scroll down a little.) – Aran-Fey Apr 27 '18 at 13:09
  • @Aran-Fey sorry I was long to respon because I did some research before asking again but I did as the recommanded in the question you linked however this does not work. It seems to work if I do not use time.wait, but I am not sure if the program closes or if it does not have time to open. I edited the question again with the last code – A.david Apr 27 '18 at 13:44
  • @Aran-Fey, I actually did some research and I do not need to absolutely use the shell = True but then the alternative would be: subprocess.Popen([pathToExcel, retFle]), but what if excel is not at the right place... with shell I only provide the name of the file that I create so I know where it is – A.david Apr 27 '18 at 13:47
  • Have you tried the [`taskkill` answer](https://stackoverflow.com/a/17614872/1222951)? Unfortunately I'm not on Windows and don't have Excel, so I can't test it. Update: Also check out the answers [here](https://stackoverflow.com/questions/696345/python-2-6-on-windows-how-to-terminate-subprocess-popen-with-shell-true-argum). – Aran-Fey Apr 27 '18 at 13:52
  • Alternatively, look into other ways to start excel like [here](https://stackoverflow.com/questions/6282230/opening-the-excel-application-from-python) or [here](https://stackoverflow.com/questions/441758/driving-excel-from-python-in-windows), so you don't have to use `shell=True`. – Aran-Fey Apr 27 '18 at 13:58
  • @Aran-Fey first thanks for your help, and yes I tried this too but it is weird nothing seems to work. No error appears but the excel window does not close... I am going to try without shell = True but I do not understand why nothing seems to work – A.david Apr 27 '18 at 13:59

0 Answers0