win32com occasionally fails to open excel file

4,793 views
Skip to first unread message

Achar

unread,
Aug 12, 2010, 8:50:50 AM8/12/10
to python-excel
If this part of code is called for many instances in single run
filePath=os.path.join(os.getcwd(),fileNameXLS)
xl = win32com.client.Dispatch("Excel.Application")
sh=xl.Workbooks.Open(filePath)
sh = wb.worksheets(worksheetNum)
sh.Cells(row,col).Value = data
wb.Save()
wb.Close()


When it tries to execute xl.Workbooks.Open(filePath)
i get the error
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py",
line 496, in
__getattr__
raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: Excel.Application.Workbooks

is there any limitation on filepath characters?

John Machin

unread,
Aug 12, 2010, 9:20:56 AM8/12/10
to python...@googlegroups.com
On 12/08/2010 10:50 PM, Achar wrote:
> If this part of code is called for many instances in single run

How many is "many"? Is it always the same number?

> filePath=os.path.join(os.getcwd(),fileNameXLS)
> xl = win32com.client.Dispatch("Excel.Application")

What is the point of executing this more than once? IIUC this is
starting up Excel each time ....

> sh=xl.Workbooks.Open(filePath)

You probably mean "wb = " in the above line.

I suggest that (1) you quote the actual code that you ran and (2) ask on
the pywin32 mailing list ... the answer is more likely to be obtained there.

@©ħα®

unread,
Aug 12, 2010, 9:25:41 AM8/12/10
to python...@googlegroups.com
def logToXLS(fileNameXLS,worksheetNum,row,col,data): 
            import win32com.client
           
            fileName=fileNameXLS     # Xls File name to be updated
            path=os.getcwd()

            filePath=os.path.join(os.getcwd(),fileNameXLS)
            xl = win32com.client.Dispatch("Excel.Application")
          
            try :
                        wb = xl.Workbooks.Open(filePath)
            except:
                     print "Failed to open spreadsheet for the path %s" %(filePath)  
            sh = wb.worksheets(worksheetNum)
            sh.Cells(row,col).Value = data
            wb.Save()
            wb.Close()

calling logToXLS() throws the mentioned error.
 File "C:\Python25\Lib\site-
packages\win32com\client\dynamic.py",
line 496, in
__getattr__
   raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: Excel.Application.Workbooks

its random behaviour .



--
You received this message because you are subscribed to the Google Groups "python-excel" group.
To post to this group, send an email to python...@googlegroups.com.
To unsubscribe from this group, send email to python-excel...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/python-excel?hl=en-GB.


John Machin

unread,
Aug 12, 2010, 7:00:16 PM8/12/10
to python...@googlegroups.com
On 12/08/2010 11:25 PM, @©ħα® wrote:
> def logToXLS(fileNameXLS,worksheetNum,row,col,data):

[snip]

>
> calling logToXLS() throws the mentioned error.
> File "C:\Python25\Lib\site-
>
> packages\win32com\client\dynamic.py",
> line 496, in
> __getattr__
> raise AttributeError, "%s.%s" % (self._username_, attr)
> AttributeError: Excel.Application.Workbooks
>
>
> its random behaviour .
>

As advised earlier, you'll get better results by asking on the
python-win32 mailing list.

Send your message to python...@python.org

Find out more about the mailing list at
http://mail.python.org/mailman/listinfo/python-win32

Reply all
Reply to author
Forward
0 new messages