[python-win32] Excel Workbooks Exception
Tim Golden
mail at timgolden.me.uk
Fri Jan 31 16:48:29 CET 2014
On 31/01/2014 15:08, Mark Mordeca wrote:
> I sometimes get a random exception when trying to open an Excel file.
> It is not consistent. I will get the exception, and then try it
> again and it will work fine.
> xl=win32com.client.DispatchEx("Excel.Application")
>
> book=xl.Workbooks.Open(self.__filename, ReadOnly=True)
>
> where self.__filename is a path to an Excel file.
>
>
>
> The exception I get is:
>
> <type 'exceptions.AttributeError'>: Excel.Application.Workbooks
>
>
>
> It’s very confusing because
>
> 1. I will get the exception, try it again, and it will work fine.
>
> 2. How is it even possible to Dispatch successfully but then it not
> have a core attribute like Workbooks?
You haven't given us a lot to work with, not even the full exception
traceback (which sometimes helps). A couple of things which *might* be
worth trying (and, at least, won't do any harm):
* Ensure that Excel has a static dispatch object generated for it; an
easy way to do this is:
win32com.client.gencache.EnsureDispatch("Excel.Application").
* Use GetObject to open a named file. (Ultimately, this is calling a
dispatch mechanism under the covers but you never know what difference
the code path might make):
xls = win32com.client.GetObject(r"c:\temp\spreadsheet.xls")
I can see no reason why either should make any real difference, but
they're worth knowing about at any rate.
TJG
More information about the python-win32
mailing list