import xlwings as xw
from shlex import split
import subprocess
res = subprocess.run(
split('tasklist /FI "IMAGENAME eq EXCEL.exe"'),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
)
all_pids = set()
for line in res.stdout.splitlines()[3:]:
# Automatically ignored if there's no processes as then it prints only 1 line
_, pid, _, _, _, _ = line.split()
all_pids.add(int(pid))
active_pids = {app.pid for app in xw.apps}
zombie_pids = all_pids - active_pids
for pid in zombie_pids:
res = subprocess.run(
split(f"taskkill /PID {pid} /F"),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
)