Using WINXP-SP2, Python 2.3
I am trying to read an excel file and then use the numbers in the excel file to do some calculations
The program reads my excel file properly but the problem comes when I try to use the numbers for example
Cell (1,1) has the number 50 in it
Now I can read it and print it but when I say for example try to add or subtract from the number it gives errors.
Here is my python code. -
from win32com.client import Dispatch
-
xlApp = Dispatch ("Excel.Application")
-
xlWb = xlApp.Workbooks.Open ("c:/fahad/new/file.xls")
-
xlSht = xlWb.Worksheets (1)
-
mainload=range(10)
-
k=0
-
for row in range(10):
-
k=k+1
-
for col in range(4):
-
if col==1:
-
load1=xlSht.Cells(k,1)
-
elif col==2:
-
load2=xlSht.Cells(k,2)
-
elif col==3:
-
load3=xlSht.Cells(k,3)
-
elif col==4:
-
load4=xlSht.Cells(k,4)
-
mainload[row]=load3
-
-
print mainload[0] + 5
-
this is the error I am getting
TypeError: unsupported operand type(s) for +: ‘instance’ and ‘float’
And whatever I am doing whether it is + or – or * or / the error respectively
TypeError: unsupported operand type(s) for -: ‘instance’ and ‘float’
TypeError: unsupported operand type(s) for *: ‘instance’ and ‘float’
TypeError: unsupported operand type(s) for /: ‘instance’ and ‘float’
What it looks like to me is that the program does not recognize whats in the cell as a number
Please help
9 3819
On line 15, for example: - load3=xlSht.Cells(k,3).Value
The "instance" of a Cells() object has other attributes as well.
bvdet 2,851
Expert Mod 2GB
Using WINXP-SP2, Python 2.3
I am trying to read an excel file and then use the numbers in the excel file to do some calculations
The program reads my excel file properly but the problem comes when I try to use the numbers for example
Cell (1,1) has the number 50 in it
Now I can read it and print it but when I say for example try to add or subtract from the number it gives errors.
Here is my python code. -
from win32com.client import Dispatch
-
xlApp = Dispatch ("Excel.Application")
-
xlWb = xlApp.Workbooks.Open ("c:/fahad/new/file.xls")
-
xlSht = xlWb.Worksheets (1)
-
mainload=range(10)
-
k=0
-
for row in range(10):
-
k=k+1
-
for col in range(4):
-
if col==1:
-
load1=xlSht.Cells(k,1)
-
elif col==2:
-
load2=xlSht.Cells(k,2)
-
elif col==3:
-
load3=xlSht.Cells(k,3)
-
elif col==4:
-
load4=xlSht.Cells(k,4)
-
mainload[row]=load3
-
-
print mainload[0] + 5
-
this is the error I am getting
TypeError: unsupported operand type(s) for +: ‘instance’ and ‘float’
And whatever I am doing whether it is + or – or * or / the error respectively
TypeError: unsupported operand type(s) for -: ‘instance’ and ‘float’
TypeError: unsupported operand type(s) for *: ‘instance’ and ‘float’
TypeError: unsupported operand type(s) for /: ‘instance’ and ‘float’
What it looks like to me is that the program does not recognize whats in the cell as a number
Please help
You can easily convert the instance object to a string and evaluate its value: - from win32com.client import Dispatch
-
xlApp = Dispatch ("Excel.Application")
-
xlWb = xlApp.Workbooks.Open('data.xls')
-
xlSht = xlWb.Worksheets (1)
-
dataList = []
-
for row in range(12,16):
-
for col in range(3,6):
-
dataList.append(xlSht.Cells(row,col))
-
-
for item in dataList:
-
print item
-
print 'Convert COMObject to text: %s' % item
Output: -
>>> 3.0
-
Convert COMObject to text: 3.0
-
901C4
-
Convert COMObject to text: 901C4
-
9.0
-
Convert COMObject to text: 9.0
-
1.0
-
Convert COMObject to text: 1.0
-
902C3
-
Convert COMObject to text: 902C3
-
9.0
-
Convert COMObject to text: 9.0
-
3.0
-
Convert COMObject to text: 3.0
-
901C5
-
Convert COMObject to text: 901C5
-
9.0
-
Convert COMObject to text: 9.0
-
1.0
-
Convert COMObject to text: 1.0
-
902C4
-
Convert COMObject to text: 902C4
-
9.0
-
Convert COMObject to text: 9.0
-
>>>
>>> float(str(dataList[0]))
3.0
>>>
You can easily convert the instance object to a string and evaluate its value: - from win32com.client import Dispatch
-
xlApp = Dispatch ("Excel.Application")
-
xlWb = xlApp.Workbooks.Open('data.xls')
-
xlSht = xlWb.Worksheets (1)
-
dataList = []
-
for row in range(12,16):
-
for col in range(3,6):
-
dataList.append(xlSht.Cells(row,col))
-
-
for item in dataList:
-
print item
-
print 'Convert COMObject to text: %s' % item
Output: -
>>> 3.0
-
Convert COMObject to text: 3.0
-
901C4
-
Convert COMObject to text: 901C4
-
9.0
-
Convert COMObject to text: 9.0
-
1.0
-
Convert COMObject to text: 1.0
-
902C3
-
Convert COMObject to text: 902C3
-
9.0
-
Convert COMObject to text: 9.0
-
3.0
-
Convert COMObject to text: 3.0
-
901C5
-
Convert COMObject to text: 901C5
-
9.0
-
Convert COMObject to text: 9.0
-
1.0
-
Convert COMObject to text: 1.0
-
902C4
-
Convert COMObject to text: 902C4
-
9.0
-
Convert COMObject to text: 9.0
-
>>>
>>> float(str(dataList[0]))
3.0
>>>
Thanks for that, bvdet. It's good to know (I don't have Excel for testing) that Cells() objects have an __str__() method hung on them. However, using the Value attribute directly sure seems to be a lot cleaner.
Would you mind testing that for me? Thanks,
Barton.
bvdet 2,851
Expert Mod 2GB
Thanks for that, bvdet. It's good to know (I don't have Excel for testing) that Cells() objects have an __str__() method hung on them. However, using the Value attribute directly sure seems to be a lot cleaner.
Would you mind testing that for me? Thanks,
Barton.
Before today I knew nothing about COMObjects, but it makes sense there would be a directly accessible attribute. - for item in dataList:
-
print item
-
print 'Print COMObject value attribute: %s' % item.value
-
-
print type(item.value)
-
>>> 3.0
-
Print COMObject value attribute: 3.0
-
901C4
-
Print COMObject value attribute: 901C4
-
9.0
-
Print COMObject value attribute: 9.0
-
1.0
-
Print COMObject value attribute: 1.0
-
902C3
-
Print COMObject value attribute: 902C3
-
9.0
-
Print COMObject value attribute: 9.0
-
3.0
-
Print COMObject value attribute: 3.0
-
901C5
-
Print COMObject value attribute: 901C5
-
9.0
-
Print COMObject value attribute: 9.0
-
1.0
-
Print COMObject value attribute: 1.0
-
902C4
-
Print COMObject value attribute: 902C4
-
9.0
-
Print COMObject value attribute: 9.0
-
<type 'float'>
-
>>>
-
Before today I knew nothing about COMObjects, but it makes sense there would be a directly accessible attribute. - for item in dataList:
-
print item
-
print 'Print COMObject value attribute: %s' % item.value
-
-
print type(item.value)
-
>>> 3.0
-
Print COMObject value attribute: 3.0
-
901C4
-
Print COMObject value attribute: 901C4
-
9.0
-
Print COMObject value attribute: 9.0
-
1.0
-
Print COMObject value attribute: 1.0
-
902C3
-
Print COMObject value attribute: 902C3
-
9.0
-
Print COMObject value attribute: 9.0
-
3.0
-
Print COMObject value attribute: 3.0
-
901C5
-
Print COMObject value attribute: 901C5
-
9.0
-
Print COMObject value attribute: 9.0
-
1.0
-
Print COMObject value attribute: 1.0
-
902C4
-
Print COMObject value attribute: 902C4
-
9.0
-
Print COMObject value attribute: 9.0
-
<type 'float'>
-
>>>
-
Very interesting... The book (©2000) has it with a capital "V" as in ".Value".
Did you try that as well?
bvdet 2,851
Expert Mod 2GB
Very interesting... The book (©2000) has it with a capital "V" as in ".Value".
Did you try that as well?
Strange, but it works.
>>> item.Value
9.0
>>> item.value
9.0
>>> item.Value2
9.0
>>> item.Address
u'$E$15'
>>> item.address
u'$E$15'
>>>
The attributes were not available through item.__dict__.
Strange, but it works.
>>> item.Value
9.0
>>> item.value
9.0
>>> item.Value2
9.0
>>> item.Address
u'$E$15'
>>> item.address
u'$E$15'
>>>
Thanks. That makes a lot of sense, now that I think about it. COM interfaces are usually specified as 'case insensitive'.
The attributes were not available through item.__dict__.
They are probably implemented as property()s. As in: -
#
-
def _get_CaptureDriverIndex(self):
-
return self.GetCOMProp('CaptureDriverIndex')
-
def _set_CaptureDriverIndex(self, CaptureDriverIndex):
-
self.SetCOMProp('CaptureDriverIndex', CaptureDriverIndex)
-
CaptureDriverIndex = property(_get_CaptureDriverIndex, _set_CaptureDriverIndex)
Just in case you are interested.
Thanks guys.
This fixed my problem: -
load3=xlSht.Cells(k,3).Value
-
And apparently it only works if the v in value is uppercase.
Thanks guys.
You are quite welcome.
This fixed my problem: -
load3=xlSht.Cells(k,3).Value
-
And apparently it only works if the v in value is uppercase.
Again, very interesting results regarding the case of the attribute name.
Thanks for keeping us up-to-date on this issue.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Allison Bailey |
last post by:
Hi Folks,
I'm a brand new Python programmer, so please point me in the right
direction if this is not the best forum for this question....
I would like to open an existing MS Excel spreadsheet...
|
by: Mark Carter |
last post by:
I use Office 2000, and I have recently upgraded to python 2.3 and
win32all-155.exe
The following code worked in python 2.2:
xlApp = Dispatch("Excel.Application")
xlApp.Visible = 1...
|
by: mbbx6spp |
last post by:
Hi All,
I already searched this newsgroup and google groups to see if I could
find a Python equivalent to Perl's Template::Extract, but didn't find
anything leading to a Python module that had...
|
by: zxo102 |
last post by:
Hi there,
I need your help for python <--> excel. I want to paste selected
cells (range) to different location on the same sheet in Excel through
python. I have tried it for a while but could not...
|
by: zxo102 |
last post by:
Hi there,
I am trying to put data including Chinese Characters into Excel
through python. But I got some problems. Here is my sample code:
##################################################
#...
|
by: John Machin |
last post by:
I am pleased to announce a new general release (0.5.2) of xlrd, a Python
package for extracting data from Microsoft Excel spreadsheets.
CHANGES:
* Book and sheet objects can now be pickled and...
|
by: e.h.doxtator |
last post by:
All
I'm a Python newbie, and I'm just getting to the wonders of COM
programming. I am trying to programmatically do the following:
1. Activate Excel
2. Add a Workbook
3. Add a Worksheet...
|
by: dan84 |
last post by:
I write my code :
#inizializzazioni varie che tralascio
#mi connetto al mio db mysql e recupero i dati che volgio inserire
nel file excel
conn = MySQLdb.connect(host = "XXX", port = XXX, user...
|
by: fordie1000 |
last post by:
Hi,
Just wondering if there is a module similar to the 'win32com' one on windows for
interacting with excel via python on a Mac?
Thanks,
|
by: Marin Brkic |
last post by:
Hello all,
please, let me apologize in advance. English is not my first language
(not even my second one), so excuse any errors with which I'm about to
embarass myself in front of the general...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |