pythoncom错误调用COM方法excel新工作表

时间:2016-12-16 14:01:00

标签: python win32com pythoncom

我一直在研究一些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

我知道我可以把try / except放在pythoncom.PumpWaitingMessage()周围,我知道它是pythonic的方式:但是我仍然有兴趣知道导致这个错误的原因...行中4次< / p>

编辑:尝试/除了泵周围没有工作以避免显示错误

0 个答案:

没有答案