3

我一直在研究一些 Python/Excel 应用程序,但是遇到了一个有趣的问题。我已将示例简化到产生错误的程度。

(警告 - 没有中断机制的无限循环)

from win32com.client import DispatchWithEvents
import msvcrt, pythoncom
import time


def TestExcel():
    class ExcelEvents:
        pass

    class WorkbookEvents:
        pass

    e = DispatchWithEvents("Excel.Application", ExcelEvents)
    e.Visible = 1
    book = e.Workbooks.Add()
    book = DispatchWithEvents(book, WorkbookEvents)

    while True:
        pythoncom.PumpWaitingMessages()
        time.sleep(0.1)


if __name__ == '__main__':
    TestExcel()
    print('Exiting')

运行时,它会打开 Excel,添加新工作簿,然后等待应用程序或工作簿对象上的事件。

如果我在 Excel 中执行任何可能的事件,则只有在将新工作表添加到工作簿时才会发生任何事情(因为事件类中没有定义处理程序)。立即产生以下错误:

pythoncom error: Python error invoking COM method.

Traceback (most recent call last):
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 278, in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 283, in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 650, in _invokeex_
    return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 586, in _invokeex_
    return func(*args)
TypeError: 'NoneType' object is not callable
pythoncom error: Python error invoking COM method.

Traceback (most recent call last):
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 278, in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 283, in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 650, in _invokeex_
    return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 586, in _invokeex_
    return func(*args)
TypeError: 'NoneType' object is not callable
pythoncom error: Python error invoking COM method.

Traceback (most recent call last):
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 278, in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 283, in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 650, in _invokeex_
    return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 586, in _invokeex_
    return func(*args)
TypeError: 'NoneType' object is not callable
pythoncom error: Python error invoking COM method.

Traceback (most recent call last):
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 278, in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 283, in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 650, in _invokeex_
    return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 586, in _invokeex_
    return func(*args)
TypeError: 'NoneType' object is not callable

我知道我可以在 pythoncom.PumpWaitingMessage() 周围放置 try/except 并且我知道这是 pythonic 方式:D 但是我仍然有兴趣知道是什么导致了这个错误......连续 4 次


编辑:尝试/除了泵周围不起作用以避免显示错误

4

0 回答 0