无法使用win32com完全关闭Excel [英] Can't close Excel completely using win32com on Python
问题描述
这是我的代码,我发现许多 VBA 的答案,.NET框架和很奇怪当执行此操作时,Excel关闭。
从win32com.client导入DispatchEx
excel = DispatchEx('Excel.Application ')
wbs = excel.Workbooks
wbs.Close()
excel.Quit()
wbs =无
excel =无#< - Excel关闭这里
但是当我执行以下操作时,它不会关闭。
excel = DispatchEx('Excel.Application')
wbs = excel.Workbooks
wb = wbs.Open('D:\ \Xaguar\\\A1.xlsm')
wb.Close(False)
wbs.Close()
excel.Quit()
wb =无
wbs =无
excel =无#< - 不关闭!
我在Stack Overflow问题中找到了一些可能的答案 交易过程在互操作后保持开放;传统方法不工作 。问题是这不是Python,我没有找到 Marshal.ReleaseComObject
和 GC
。我查看了 ... site-packages / win32com
和其他人的所有演示。
即使不打扰我,如果我可以得到PID并杀死它。
我发现了一个解决方法在 基于窗口名称(win32)的杀死进程 p>
可能不是正确的方法,但一个工作是:
def close_excel_by_force(excel):
import win32process
import win32gui
import win32api
import win32con
#获取窗口的进程ID
hwnd = excel 。hwnd
t,p = win32process.GetWindowThreadProcessId(hwnd)
#请求窗口很好地关闭
win32gui.PostMessage(hwnd,win32con.WM_CLOSE,0,0)
#允许一些应用程序关闭时间
time.sleep(10)
#如果应用程序没有关闭,强制关闭
尝试:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE,0,p)
如果句柄:
win32api.TerminateProcess(handle,0)
win32api.CloseHandle(handle)
except:
pass
excel = DispatchEx('Excel.Application')
wbs = excel.Workbooks
wb = wbs.Open('D:\\Xaguar\\ \\\A1.xlsm')
wb.Close(False)
wbs.Close()
excel.Quit()
wb =无
wbs =无
close_excel_by_force(excel)#< ---你#@#$#DIEEEEE! DIEEEE!
尝试这样:
wbs.Close()
excel.Quit()
del excel#这行从我的case中的任务管理器中删除
This is my code, and I found many answers for VBA, .NET framework and is pretty strange. When I execute this, Excel closes.
from win32com.client import DispatchEx
excel = DispatchEx('Excel.Application')
wbs = excel.Workbooks
wbs.Close()
excel.Quit()
wbs = None
excel = None # <-- Excel Closes here
But when I do the following, it does not close.
excel = DispatchEx('Excel.Application')
wbs = excel.Workbooks
wb = wbs.Open('D:\\Xaguar\\A1.xlsm')
wb.Close(False)
wbs.Close()
excel.Quit()
wb = None
wbs = None
excel = None # <-- NOT Closing !!!
I found some possible answer in Stack Overflow question Excel process remains open after interop; traditional method not working. The problem is that is not Python, and I don't find Marshal.ReleaseComObject
and GC
. I looked over all the demos on ...site-packages/win32com
and others.
Even it does not bother me if I can just get the PID and kill it.
I found a workaround in Kill process based on window name (win32).
May be not the proper way, but a workround is:
def close_excel_by_force(excel):
import win32process
import win32gui
import win32api
import win32con
# Get the window's process id's
hwnd = excel.Hwnd
t, p = win32process.GetWindowThreadProcessId(hwnd)
# Ask window nicely to close
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
# Allow some time for app to close
time.sleep(10)
# If the application didn't close, force close
try:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
if handle:
win32api.TerminateProcess(handle, 0)
win32api.CloseHandle(handle)
except:
pass
excel = DispatchEx('Excel.Application')
wbs = excel.Workbooks
wb = wbs.Open('D:\\Xaguar\\A1.xlsm')
wb.Close(False)
wbs.Close()
excel.Quit()
wb = None
wbs = None
close_excel_by_force(excel) # <--- YOU #@#$# DIEEEEE!! DIEEEE!!!
Try this:
wbs.Close()
excel.Quit()
del excel # this line removed it from task manager in my case
这篇关于无法使用win32com完全关闭Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!