简体   繁体   English

win32com.client Python - Excel 任务在告诉应用程序退出后仍然运行

[英]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.

相关问题 如何使用 python win32com.client 在 Excel 中添加折线? - How to Add polyline in Excel with python win32com.client? 如何关闭另一个应用程序打开的 Excel 文件? 将 python 与 win32com.client 一起使用 - How to close an Excel file opened by another application? Using python with win32com.client 使用win32com.client打开excel窗口后需要最大化 - Need to maximize the excel window after opening it using win32com.client 如何选择win32com.client必须在python中使用的excel版本? - How to choose the version of excel which win32com.client has to use in python? 用于Select-Case(excel)的Python Win32com.Client模块中的语法是什么 - What is the syntax in Python Win32com.Client module for Select-Case (excel) 如何使用 win32com.client 通过 python 将 XLA 添加到 excel 中? - How do I get an XLA add in into excel through python using win32com.client? 如何使用Python和win32com.client将数据添加到Excel图表中 - How do I add data to an Excel chart using Python and win32com.client 使用win32com.client计算Excel文件中的页数 - Count number of sheets in an Excel file using win32com.client 如何通过使用“ win32com.client as win32”来知道excel使用的行数和列数 - how to know used row count and column count of excel by using “win32com.client as win32” Python和win32com.client似乎缺少方法,特别是SetSourceData - Python and win32com.client seem to be missing methods, in particular SetSourceData
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM