occasional win32com error

Ransom gyows at sbcglobal.net
Thu Jun 15 20:04:23 CEST 2006


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!




More information about the Python-list mailing list