Reading floats from Excel using COM

Alagalah keith at nekotaku.com
Tue Jul 14 20:29:43 EDT 2009


Hi, I am using the Range function to return a tuple from Excel.

The data in the range (nREVENUE) in the excel file is 100.0, 101.0,
102.0, 103.0, 104.0

I can successfully iterate across the tuple and list, but when I try
and cast to a float to do some math, I get:

  File "C:\Python25\lib\site-packages\win32com\client\__init__.py",
line 454, in __getattr__
    raise AttributeError, "'%s' object has no attribute '%s'" % (repr
(self), attr)
AttributeError: '<win32com.gen_py.Microsoft Excel 12.0 Object
Library.Range instance at 0x37766608>' object has no attribute
'__float__'

**** CODE *****
import win32com.client
import os

excel=win32com.client.Dispatch("Excel.Application")
excel.Visible=0
excel.DisplayAlerts=False

#This is the file that contains the macro
xlsname=os.path.join(os.getcwd(),"nametest.xlsx")
nametest=excel.Workbooks.Open(xlsname)

revenue_list=excel.Range("nREVENUE")

revenue=[]
revenue.extend(revenue_list)

for i in range(len(revenue)):
    rev=revenue[i]
    print float(rev)

excel.Quit()
del excel

****************

I need to use COM as I will eventually need formula support, otherwise
I would use xlutils.

Any advice on how to cast the elements to a float would be greatly
appreciated!



More information about the Python-list mailing list