Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';在Python上使用win32com完全关闭Excel_Python_Excel_Com_Pywin32_Win32com - Fatal编程技术网

Can';在Python上使用win32com完全关闭Excel

Can';在Python上使用win32com完全关闭Excel,python,excel,com,pywin32,win32com,Python,Excel,Com,Pywin32,Win32com,这是我的代码,我为.NET framework找到了很多答案,这很奇怪。执行此操作时,Excel将关闭 from win32com.client import DispatchEx excel = DispatchEx('Excel.Application') wbs = excel.Workbooks wbs.Close() excel.Quit() wbs = None excel = None # <-- Excel Closes here 我在使用Excel的文件中有以下内容:

这是我的代码,我为.NET framework找到了很多答案,这很奇怪。执行此操作时,Excel将关闭

from win32com.client import DispatchEx
excel = DispatchEx('Excel.Application')
wbs = excel.Workbooks
wbs.Close()
excel.Quit()
wbs = None
excel = None # <-- Excel Closes here

我在使用Excel的文件中有以下内容:

self.excel.Application.Quit()
试试这个:

wbs.Close()
excel.Quit()
del excel # this line removed it from task manager in my case

对我来说,有效的方法是确保取消引用沿途分配的任何变量,如下所示:

import win32com.client as win32

fileDirectory = 'Hello World.xlsx'

#excelFile = win32.Dispatch('Excel.Application')
excelFile = win32.gencache.EnsureDispatch('Excel.Application')

excelFile.Visible = True
excelFile.DisplayAlerts = True

wb = excelFile.Workbooks.Open(fileDirectory)
ws = wb.Sheets("Sell")

ws.Range("D1").Value = "Hello World!"
ws = None

wb.Close(False)
wb = None

excelFile.Quit()
excelFile = None

它可以使用任何一种分派格式。

Python应该处理COM对象的生命周期。只需设置
excel=None
。请参见以下参考:

# CH9 says: Python manages COM lifetimes automatically for you; when your excel 
#           variable is no longer used, Excel automatically closes. In Python,
#           the simplest way to remove this variable is to assign it to another 
#           value. If you use the following code, notice that Excel vanishes 
#           from the screen; it knows there are no longer any programs referring to it. 
src:


我喜欢通过win32com杀死Excel进程并不总是那么可靠。其中包括上文建议的
excel.Application.Quit()
,以及
del excel

我发现确保它死机的唯一方法是物理杀死
EXCEL.EXE
进程:

import psutil

def kill_excel():
    for proc in psutil.process_iter():
        if proc.name() == "EXCEL.EXE":
            proc.kill()

缺点是该脚本显然会杀死当前运行的所有excel进程。如果你能接受这一点,那么这是一个不错的选择。

尝试在调用wbs之前添加一行
excel.DisplayAlerts=False
。打开(…),看看这是否有帮助。:/不工作,是一个真正的痛苦excel和COMI找到了方法,但没有使用Com更快、更干净、更简单的答案在这里:你在使用DispatchEx吗,或者调度,它真的关闭了还是继续运行?因为事实上,当我关闭它时,它似乎已关闭,但当我看到任务管理器时,它仍保持活动状态,对此表示抱歉。我使用的是可能会有所不同的补丁。之后它将完全关闭。del excel不会从任务管理器中删除excel进程。请将wbs.Close()更改为wbs.Close(False)以关闭工作簿而不保存。
# CH9 says: Python manages COM lifetimes automatically for you; when your excel 
#           variable is no longer used, Excel automatically closes. In Python,
#           the simplest way to remove this variable is to assign it to another 
#           value. If you use the following code, notice that Excel vanishes 
#           from the screen; it knows there are no longer any programs referring to it. 
import psutil

def kill_excel():
    for proc in psutil.process_iter():
        if proc.name() == "EXCEL.EXE":
            proc.kill()