occasional win32com error

Roger Upole rupole at hotmail.com
Fri Jun 23 00:34:28 EDT 2006


You could try using DispatchEx to make sure you always
get a new instance of Excel.

       Roger

"Ransom" <gyows at sbcglobal.net> wrote in message news:1150394663.125760.231580 at f6g2000cwb.googlegroups.com...
> Hey folks...
>
> Newbie here. I'm working with win32com launching, closing and
> re-launching Excel grabbing output and doing stuff.
>
> Well, on some occasions, I get the following error:
>
>
> Traceback (most recent call last):
>  File "checkrates.py", line 95, in ?
>    newdata = getNewData(testcases1)
>  File "checkrates.py", line 62, in getNewData
>    excel.Workbooks.Open(docdir + 'TOSrat2006_09.xls')
>  File "C:\Python24\lib\site-packages\win32com\client\dynamic.py", line
> 496, in __getattr__
>    raise AttributeError, "%s.%s" % (self._username_, attr)
> AttributeError: Excel.Application.Workbooks
>
> Sometimes the code runs fine. Sometimes I get this error. The code in
> question is:
>
> import string
> from win32com.client import Dispatch
> docdir = 'E:\\scripts\\Python\\RSAutomation\\'
>
>
> def getOldData(testcases):
>   #open excel
>    excel = Dispatch("Excel.Application")
>    excel.Workbooks.Open(docdir + 'TOSrat2006_07.xls')
>
>    oldoutputlist = []
>    for rsinput in testcases.xreadlines():
>
>        inputlist = string.split(rsinput, ',')
>
>        # iterate through and update spreadheet input
>        cellx = range(3,51)
>        values = range(0,48)
>        for i,r in zip(cellx, values):
>
>            excel.ActiveSheet.Cells(i,2).Value = inputlist[r]
>
>        #read spreadsheet output and cat to outputlist
>        premium = excel.ActiveSheet.Cells(32,6).Value
>        oldoutputlist.append(premium)
>    # close up excel
>    excel.ActiveWorkbook.Close(SaveChanges=0)
>    excel.Quit()
>    del excel
>    return oldoutputlist
>
> def getNewData(testcases):
>    # open excel
>    excel = Dispatch("Excel.Application")
>    excel.Workbooks.Open(docdir + 'TOSrat2006_09.xls')
>    newoutputlist = []
>    for rsinput in testcases.xreadlines():
>
>        inputlist = string.split(rsinput, ',')
>
>        # iterate through and update spreadsheet input
>        cellx = range(3,51)
>        values = range(0,48)
>        for i,r in zip(cellx, values):
>
>            excel.ActiveSheet.Cells(i,2).Value = inputlist[r]
>
>        # read ratesheet output and cat to outputlist
>        premium = excel.ActiveSheet.Cells(32,6).Value
>        newoutputlist.append(premium)
>    excel.ActiveWorkbook.Close(SaveChanges=0)
>    excel.Quit()
>    del excel
>    return newoutputlist
>
> if __name__ == "__main__":
>    testcases = open('arse_testcases.csv','r')
>    testcases1 = open('arse_testcases.csv','r')
>    olddata = getOldData(testcases)
>    newdata = getNewData(testcases1)
>
>    print olddata
>    print newdata
>
>
> It seems like Python or COM is having a hard time freeing up (or
> closing down) excel prior to the "getNewData" function running and it
> is stepping on itself. I thought the stuff I'm doing at the end of
> getOldData should successfully shut down excel.
>
> Any advice, criticism, flames are appreciated.
>
> Cheers!
> 




----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---



More information about the Python-list mailing list