occasional win32com error

R

Ransom

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!
 
R

Roger Upole

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

Roger

Ransom said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top