Usually I use the following code to open an excel workbook in the background:
import xlwings as xw
app = xw.App(visible=False)
wb = xw.Book(filename)
sheet = wb.sheets['sheet1']
I sometimes do not code correctly and get an error message when I execute a code containing the above lines (with visible=False
). In this case, the EXCEL.EXE
process stays open on the processes list (in windows task manager on windows 10) in the background.
Is there a solution that closes the particular excel process in the background that is opend with the python code, if I receive an error message? Otherwise for each time the code is executed with an error one extra excel process gets added to the process list resulting in less performance.
Currently, my workaround is to add the following lines at the top of the python script, but this closes all excel processes:
import subprocess
subprocess.call(["taskkill", "/f", "/im", "EXCEL.EXE"])
My objective is to close only that particular process that is opend with the python script.
app.quit()
– Unroot