python关闭excel进程_无法使用win32com on Python完全关闭Excel

当使用win32com在Python中打开并操作Excel文件后,发现正常方式无法完全关闭Excel进程。通过尝试不同的方法,包括调用`Quit()`和设置变量为`None`,问题仍未解决。最终,通过引入`win32process`、`win32gui`和`win32api`模块,使用强制关闭进程的技巧,实现了即使应用程序没有响应也能关闭Excel进程的功能。在代码中添加`del excel`语句可以将Excel实例从任务管理器中移除。
摘要由CSDN通过智能技术生成

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 #

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 #

I found some possible answer in Stack Overflow question

Even it does not bother me if I can just get the PID and kill it.

May be not the proper way, but a workround is:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值