xlwings Workbook() call returns AttributeError: Op

2019-08-04 01:34发布

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!

0条回答
登录 后发表回答