convert COM obj to integer

B

Bell, Kevin

I'm pulling a range of cells from Excel into a list and the data in
Excel is a number or possibly some text like an asterisk. Each member
of the list is a com object (I think) and I'm converting them to
integers (or to None if not numberic) but my method seems very silly.
Is this the best way to go about it?

It does exactly what it should, but it seems like a lot of extra BS to
convert my object to a string to a float to a rounded integer! Don't
laugh. I'm new at this!


THE SCRIPT:----------------------------------

import win32com.client
xlApp = win32com.client.Dispatch("Excel.Application")
f = r"C:\py\TrafficVolumes\xlTestDocs\3125113A.xls"
xlApp.Visible = 0
xlApp.Workbooks.Open(f)
list = xlApp.ActiveWorkbook.ActiveSheet.Range("Q13:Q36")

print list
print "\n"


def comObjToInteger(myObj):
try:
s = str(myObj)
fl = float(s)
integer = int(round(fl))
return integer
except:
return None


for i in list:
print comObjToInteger(i)


xlApp.ActiveWorkbook.Close(SaveChanges=0)
xlApp.Quit()

del xlApp #clean up
----------------------------------------------

THE RESULT:--------------------
((4.7999999999999998,), (u'*',), (2.0,), (1.6000000000000001,),
(5.5999999999999996,), (19.399999999999999,), (25.0,),
(38.799999999999997,), (32.799999999999997,), (21.0,), (24.0,),
(17.399999999999999,), (22.800000000000001,), (22.600000000000001,),
(33.799999999999997,), (35.399999999999999,), (29.199999999999999,),
(35.399999999999999,), (32.200000000000003,), (26.0,),
(24.399999999999999,), (22.800000000000001,), (14.0,), (11.6,))

my converted values:
5
None
2
2
6
19
25
39
33
21
24
17
23
23
34
35
29
35
32
26
24
23
14
12
----------------------------------
 
S

Steve M

I don't know exactly what a COM object is, but those aren't them. The
win32com package takes care of converting everything to Python types.
The excel call returns a tuple of tuples. That is, the outer tuple is
the sequence of rows, and each such row is itself a tuple with one
member per column requested. Since you only request one column, it is a
one-item-long tuple, also called a 1-tuple. That is demonstrated by the
result of print'ing the list.

By the way, you shouldn't use 'list' as a name because it is also the
name of a built-in function. And it isn't a list anyway, it's a tuple.

Now, each number is in fact already a primitive Python object of type
float. (The asterisk is a unicode string.) So you want to convert the
floats into integers, and it looks like you want to round rather than
truncate.
----
table = xlApp.ActiveWorkbook.ActiveSheet.Range("Q13:Q36")

converted_values = []

for row in table:
value = row[0] #get the first (and only) item in the tuple
try:
value = round(value)
except TypeError: #value is not a float
value = None
else:
value = int(value) #turn the float into an int
converted_values.append(value)
print converted_values
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top