473,416 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Python and Excel

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.

Expand|Select|Wrap|Line Numbers
  1. from win32com.client import Dispatch
  2. xlApp = Dispatch ("Excel.Application")
  3. xlWb = xlApp.Workbooks.Open ("c:/fahad/new/file.xls")
  4. xlSht = xlWb.Worksheets (1)
  5. mainload=range(10)
  6. k=0
  7. for row in range(10):
  8.   k=k+1
  9.   for col in range(4):
  10.       if col==1:
  11.         load1=xlSht.Cells(k,1)
  12.       elif col==2:
  13.         load2=xlSht.Cells(k,2)
  14.       elif col==3:
  15.         load3=xlSht.Cells(k,3)
  16.       elif col==4:
  17.         load4=xlSht.Cells(k,4)
  18.   mainload[row]=load3
  19.  
  20. print mainload[0] + 5
  21.  

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
Oct 1 '07 #1
9 3819
bartonc
6,596 Expert 4TB
On line 15, for example:
Expand|Select|Wrap|Line Numbers
  1. load3=xlSht.Cells(k,3).Value
The "instance" of a Cells() object has other attributes as well.
Oct 1 '07 #2
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.

Expand|Select|Wrap|Line Numbers
  1. from win32com.client import Dispatch
  2. xlApp = Dispatch ("Excel.Application")
  3. xlWb = xlApp.Workbooks.Open ("c:/fahad/new/file.xls")
  4. xlSht = xlWb.Worksheets (1)
  5. mainload=range(10)
  6. k=0
  7. for row in range(10):
  8.   k=k+1
  9.   for col in range(4):
  10.       if col==1:
  11.         load1=xlSht.Cells(k,1)
  12.       elif col==2:
  13.         load2=xlSht.Cells(k,2)
  14.       elif col==3:
  15.         load3=xlSht.Cells(k,3)
  16.       elif col==4:
  17.         load4=xlSht.Cells(k,4)
  18.   mainload[row]=load3
  19.  
  20. print mainload[0] + 5
  21.  

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:
Expand|Select|Wrap|Line Numbers
  1. from win32com.client import Dispatch
  2. xlApp = Dispatch ("Excel.Application")
  3. xlWb = xlApp.Workbooks.Open('data.xls')
  4. xlSht = xlWb.Worksheets (1)
  5. dataList = []
  6. for row in range(12,16):
  7.     for col in range(3,6):
  8.             dataList.append(xlSht.Cells(row,col))
  9.  
  10. for item in dataList:
  11.     print item
  12.     print 'Convert COMObject to text: %s' % item
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> 3.0
  2. Convert COMObject to text: 3.0
  3. 901C4
  4. Convert COMObject to text: 901C4
  5. 9.0
  6. Convert COMObject to text: 9.0
  7. 1.0
  8. Convert COMObject to text: 1.0
  9. 902C3
  10. Convert COMObject to text: 902C3
  11. 9.0
  12. Convert COMObject to text: 9.0
  13. 3.0
  14. Convert COMObject to text: 3.0
  15. 901C5
  16. Convert COMObject to text: 901C5
  17. 9.0
  18. Convert COMObject to text: 9.0
  19. 1.0
  20. Convert COMObject to text: 1.0
  21. 902C4
  22. Convert COMObject to text: 902C4
  23. 9.0
  24. Convert COMObject to text: 9.0
  25. >>>
>>> float(str(dataList[0]))
3.0
>>>
Oct 1 '07 #3
bartonc
6,596 Expert 4TB
You can easily convert the instance object to a string and evaluate its value:
Expand|Select|Wrap|Line Numbers
  1. from win32com.client import Dispatch
  2. xlApp = Dispatch ("Excel.Application")
  3. xlWb = xlApp.Workbooks.Open('data.xls')
  4. xlSht = xlWb.Worksheets (1)
  5. dataList = []
  6. for row in range(12,16):
  7.     for col in range(3,6):
  8.             dataList.append(xlSht.Cells(row,col))
  9.  
  10. for item in dataList:
  11.     print item
  12.     print 'Convert COMObject to text: %s' % item
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> 3.0
  2. Convert COMObject to text: 3.0
  3. 901C4
  4. Convert COMObject to text: 901C4
  5. 9.0
  6. Convert COMObject to text: 9.0
  7. 1.0
  8. Convert COMObject to text: 1.0
  9. 902C3
  10. Convert COMObject to text: 902C3
  11. 9.0
  12. Convert COMObject to text: 9.0
  13. 3.0
  14. Convert COMObject to text: 3.0
  15. 901C5
  16. Convert COMObject to text: 901C5
  17. 9.0
  18. Convert COMObject to text: 9.0
  19. 1.0
  20. Convert COMObject to text: 1.0
  21. 902C4
  22. Convert COMObject to text: 902C4
  23. 9.0
  24. Convert COMObject to text: 9.0
  25. >>>
>>> 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.
Oct 1 '07 #4
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.
Expand|Select|Wrap|Line Numbers
  1. for item in dataList:
  2.     print item
  3.     print 'Print COMObject value attribute: %s' % item.value
  4.  
  5. print type(item.value)
Expand|Select|Wrap|Line Numbers
  1. >>> 3.0
  2. Print COMObject value attribute: 3.0
  3. 901C4
  4. Print COMObject value attribute: 901C4
  5. 9.0
  6. Print COMObject value attribute: 9.0
  7. 1.0
  8. Print COMObject value attribute: 1.0
  9. 902C3
  10. Print COMObject value attribute: 902C3
  11. 9.0
  12. Print COMObject value attribute: 9.0
  13. 3.0
  14. Print COMObject value attribute: 3.0
  15. 901C5
  16. Print COMObject value attribute: 901C5
  17. 9.0
  18. Print COMObject value attribute: 9.0
  19. 1.0
  20. Print COMObject value attribute: 1.0
  21. 902C4
  22. Print COMObject value attribute: 902C4
  23. 9.0
  24. Print COMObject value attribute: 9.0
  25. <type 'float'>
  26. >>>
  27.  
Oct 1 '07 #5
bartonc
6,596 Expert 4TB
Before today I knew nothing about COMObjects, but it makes sense there would be a directly accessible attribute.
Expand|Select|Wrap|Line Numbers
  1. for item in dataList:
  2.     print item
  3.     print 'Print COMObject value attribute: %s' % item.value
  4.  
  5. print type(item.value)
Expand|Select|Wrap|Line Numbers
  1. >>> 3.0
  2. Print COMObject value attribute: 3.0
  3. 901C4
  4. Print COMObject value attribute: 901C4
  5. 9.0
  6. Print COMObject value attribute: 9.0
  7. 1.0
  8. Print COMObject value attribute: 1.0
  9. 902C3
  10. Print COMObject value attribute: 902C3
  11. 9.0
  12. Print COMObject value attribute: 9.0
  13. 3.0
  14. Print COMObject value attribute: 3.0
  15. 901C5
  16. Print COMObject value attribute: 901C5
  17. 9.0
  18. Print COMObject value attribute: 9.0
  19. 1.0
  20. Print COMObject value attribute: 1.0
  21. 902C4
  22. Print COMObject value attribute: 902C4
  23. 9.0
  24. Print COMObject value attribute: 9.0
  25. <type 'float'>
  26. >>>
  27.  
Very interesting... The book (©2000) has it with a capital "V" as in ".Value".
Did you try that as well?
Oct 1 '07 #6
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__.
Oct 2 '07 #7
bartonc
6,596 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. #
  2.     def _get_CaptureDriverIndex(self):
  3.         return self.GetCOMProp('CaptureDriverIndex')
  4.     def _set_CaptureDriverIndex(self, CaptureDriverIndex):
  5.         self.SetCOMProp('CaptureDriverIndex', CaptureDriverIndex)
  6.     CaptureDriverIndex = property(_get_CaptureDriverIndex, _set_CaptureDriverIndex)
Just in case you are interested.
Oct 2 '07 #8
Thanks guys.

This fixed my problem:

Expand|Select|Wrap|Line Numbers
  1. load3=xlSht.Cells(k,3).Value
  2.  
And apparently it only works if the v in value is uppercase.
Oct 2 '07 #9
bartonc
6,596 Expert 4TB
Thanks guys.
You are quite welcome.
This fixed my problem:

Expand|Select|Wrap|Line Numbers
  1. load3=xlSht.Cells(k,3).Value
  2.  
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.
Oct 2 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

13
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...
1
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...
5
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...
3
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...
1
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: ################################################## #...
15
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...
4
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...
0
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...
0
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,
20
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...
0
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...

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.