[python-win32] Excel Workbooks Exception
Mark Mordeca
mark.mordeca at prosensus.ca
Fri Jan 31 17:30:26 CET 2014
Thanks for your reply. Unfortunately, the traceback isn't helpful....
it's just pointing to lines in my own code that arrive at calling these
two lines because I re-raise exceptions (I'm not going to get into the
details of this, it is just a fact of this part of code I have to deal
with because this particular type of exception is not ever expected).
Thanks for your two alternate ways to create the Excel instance, I have a
question about them though. I use DispatchEx because I have to create a
new instance of Excel and open the Excel file in that instance, in all
cases. Does EnsureDispatch or GetObject always create new instances of
Excel? If not, is there a flag to either of these that will make them do
so? (Sorry, I am having trouble finding documentation on these two
methods).
Thanks.
-----Original Message-----
From: python-win32
[mailto:python-win32-bounces+mark.mordeca=prosensus.ca at python.org] On
Behalf Of Tim Golden
Sent: January-31-14 10:48 AM
To: python-win32 at python.org
Subject: Re: [python-win32] Excel Workbooks Exception
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
_______________________________________________
python-win32 mailing list
python-win32 at python.org
https://mail.python.org/mailman/listinfo/python-win32
More information about the python-win32
mailing list