[英]win32com.client Python - Excel task remains running after telling application to quit
I've seen a couple questions similar to this, but they both appear to involve VBA and not Python.我见过几个与此类似的问题,但它们似乎都涉及 VBA 而不是 Python。
This is a relatively recent error, so I suspect it might have something to do with the fact that I'm using Python 3.7 now.这是一个相对较新的错误,所以我怀疑它可能与我现在使用 Python 3.7 的事实有关。
Basically, using the Dispatch method from win32com.client, I am able to open a new Excel workbook and make my edits as I always have been.基本上,使用 win32com.client 中的 Dispatch 方法,我可以打开一个新的 Excel 工作簿并像往常一样进行编辑。 However, for some reason, I am unable to tell the application to quit successfully.
但是,由于某种原因,我无法告诉应用程序成功退出。
Used to be that I could write: self.excel_app.Quit()
曾经是我可以写:
self.excel_app.Quit()
But now, I'm getting an AttributeError, of all things.但是现在,我得到了一个 AttributeError,所有的事情。 Saying the Excel.Application does not have a Quit() attribute.
说 Excel.Application 没有 Quit() 属性。 Again, this is Python 3.7.
同样,这是 Python 3.7。 What happened?
发生了什么?
[EDIT] Relevant code: [编辑] 相关代码:
import sys
from win32com.client import Dispatch
#...
class X(object):
def __init__(self):
#...
self.excel_app = Dispatch("Excel.Application")
self.report_workbook = self.excel_app.Workbooks.Add()
#...
def close_excel(self):
try:
self.excel_app.Quit()
except Exception as ex:
sys.stdout.write("Could not quit application.\n-> ({}) {}\n".format(ex.__class__.__name__, ex))
self.excel_app = None
The exception printed to the terminal is:打印到终端的异常是:
Could not quit application.
(AttributeError) Excel.Application.Quit
Turns out that my error was coming from the fact that I was still using threading.Thread as the base class for the object trying to do this.事实证明,我的错误来自我仍在使用 threading.Thread 作为 object 尝试执行此操作的基础 class 的事实。 Apparently it doesn't work so well with the Dispatch Excel objects anymore, and I'm trying to move away from Threads anyway.
显然,它不再适用于 Dispatch Excel 对象,无论如何我都在尝试远离线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.