xlwings Workbook() call returns AttributeError: Open.Application

倖福魔咒の 提交于 2019-12-11 10:43:54

问题


Hope you can help me out, first question I ask myself but I am always impressed by the professional answers I find here!

I am using xlwings to both read and write data from .xls files. I am not a seasoned programmer, and I make mistakes. From time to time, this means rebooting processes, Python and what-not.

Every now and again, I would get the following AttributeError upon calling Workbook(). The real problem is not that I don't have a clue what is happening (I tried reading the modules the error diagnostics reference to, but they are written on a level of Python that is beyond my skills), but that restarting and even rebooting(!) my laptop don't (always) solve the problem.

I wish I could give some clues as to what is happening, but it appears to be a randomly occurring problem and right now, three reboots and several restarts didn't fix it. Google can't help either. I must have run my program a hundred times by now and it wasn't always a problem! The error message, upon simply calling Workbook():

wb = Workbook()

Traceback (most recent call last):

File "<ipython-input-22-4a3c36eb9bf9>", line 1, in <module>
wb = Workbook()

File "C:\Program Files\Anaconda\lib\site-packages\xlwings\main.py", line 141, in __init__
self.xl_app, self.xl_workbook = xlplatform.new_workbook()

File "C:\Program Files\Anaconda\lib\site-packages\xlwings\_xlwindows.py", line 104, in new_workbook
xl_app = _get_latest_app()

File "C:\Program Files\Anaconda\lib\site-packages\xlwings\_xlwindows.py", line 88, in _get_latest_app
return xl_workbook_current.Application

File "C:\Program Files\Anaconda\lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))

AttributeError: Open.Application

And as a follow up, the piece of code from 'dyamic.py' raising the error:

[...]
# If we are still here, and have a retEntry, get the OLE item
    if not retEntry is None:
        invoke_type = _GetDescInvokeType(retEntry, pythoncom.INVOKE_PROPERTYGET)
        debug_attr_print("Getting property Id 0x%x from OLE object" % retEntry.dispid)
        try:
            ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
        except pythoncom.com_error, details:
            if details.hresult in ERRORS_BAD_CONTEXT:
                # May be a method.
                self._olerepr_.mapFuncs[attr] = retEntry
                return self._make_method_(attr)
            raise
        debug_attr_print("OLE returned ", ret)
        return self._get_good_object_(ret)

    # no where else to look.
    raise AttributeError("%s.%s" % (self._username_, attr))

As I mentioned before, the last bit it too much for me :P

I guessed some lingering Excel.exe (or similar) process was interfering with the script after I closed some random window due to some random error, but I cant find one in the task manager. Also, this is the kind of thing a reboot should fix, right?! I also upgraded to xlwings .34 (latest version, as of now).

Windows 8.1 64 bit, Python 2.7 (Anaconda distribution, Spyder IDE), Excel 2013.

Any help is very much appreciated, of course!

Thanks a lot, kind regards,

Gordon.

EDIT:

Following advice given in the comments, I can now report that even at times when wb = Workbook() fails, from win32com.client import dynamic followed by dynamic.Dispatch('Excel.Application') returns <COMObject Excel.Application>.

Hope it helps!

来源:https://stackoverflow.com/questions/29349050/xlwings-workbook-call-returns-attributeerror-open-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!