how to kill all excel process before code running by xlwings
时间: 2024-05-13 08:14:09 浏览: 94
You can use the following code to kill all Excel processes before running your code using xlwings:
```
import psutil
def kill_excel_processes():
for proc in psutil.process_iter():
try:
if proc.name() == "EXCEL.EXE":
proc.kill()
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
# Call the kill_excel_processes function before running your xlwings code
kill_excel_processes()
```
This code uses the `psutil` module to iterate through all running processes and kills any process with the name "EXCEL.EXE". Make sure to call the `kill_excel_processes()` function before running your xlwings code to ensure that any existing Excel processes are terminated.
阅读全文