471,089 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

occasional win32com error

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!

Jun 15 '06 #1
1 4582
You could try using DispatchEx to make sure you always
get a new instance of Excel.

Roger

"Ransom" <gy***@sbcglobal.net> wrote in message news:11**********************@f6g2000cwb.googlegro ups.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 =---
Jun 23 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Justin Stockton | last post: by
1 post views Thread by Cristian Hasbun from Chile | last post: by
reply views Thread by goermezer | last post: by
1 post views Thread by g.franzkowiak | last post: by
2 posts views Thread by ago | last post: by
4 posts views Thread by mirandacascade | last post: by
11 posts views Thread by Bill Davy | last post: by
2 posts views Thread by RyanN | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.