Python Forum
Getting Cells from the Sheets "automate the boring stuff"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting Cells from the Sheets "automate the boring stuff"
#1
I am working through the exel chapter of Automate the Boring Stuff. I am typing exactly as the book is telling me on page 268 and getting an error message.
>>> import openpyxl
>>> wb = openpyxl.load_workbook('Example_Page267.xlsx')
>>> sheet = wb.get_sheet_by_name('Sheet1')
>>> sheet['A1']
<Cell 'Sheet1'.A1>
>>> sheet['A1'].value
datetime.datetime(2015, 5, 4, 13, 34)
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + int(c.row) + ',Column ' + c.column + ' is ' + c.value
i then get the following error
Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
    'Row ' + int(c.row) + ',Column ' + c.column + ' is ' + c.value
TypeError: can only concatenate str (not "int") to str
Reply
#2
It looks like an error, int(c.row) should probably be str(c.row)
Reply
#3
hi, I have tried that and get the same error.

sorry
i typed the original message wrong
>>> import openpyxl
>>> wb = openpyxl.load_workbook('Example_Page267.xlsx')
>>> sheet = wb.get_sheet_by_name('Sheet1')
>>> sheet['A1']
<Cell 'Sheet1'.A1>
>>> sheet['A1'].value
datetime.datetime(2015, 5, 4, 13, 34)
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str(c.row) + ',Column ' + c.column + ' is ' + c.value
this is actually what i typed and get the same error
Reply
#4
Shafla Wrote:and get the same error
It is very important that you paste the entire content of the error message printed by python in the forum. It is the best way to get some help.
Reply
#5
I have tried it once more and got the following
>>> import openpyxl
>>> wb = openpyxl.load_workbook('Example_Page267.xlsx')
>>> sheet = wb.get_sheet_by_name('Sheet1')
>>> sheet['A1']
<Cell 'Sheet1'.A1>
>>> sheet['A1'].value
datetime.datetime(2015, 5, 4, 13, 34)
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str(c.row) + ',Column ' + c.column + ' is ' + c.value
Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    'Row ' + str(c.row) + ',Column ' + c.column + ' is ' + c.value
TypeError: can only concatenate str (not "int") to str
Reply
#6
(Sep-23-2019, 09:02 PM)Shafla Wrote: I have tried it once more and got the following
>>> import openpyxl
>>> wb = openpyxl.load_workbook('Example_Page267.xlsx')
>>> sheet = wb.get_sheet_by_name('Sheet1')
>>> sheet['A1']
<Cell 'Sheet1'.A1>
>>> sheet['A1'].value
datetime.datetime(2015, 5, 4, 13, 34)
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str(c.row) + ',Column ' + c.column + ' is ' + c.value
Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    'Row ' + str(c.row) + ',Column ' + c.column + ' is ' + c.value
TypeError: can only concatenate str (not "int") to str
Hi!

Just a thought ... If rows need to be strings, maybe columns too ...
Have you tried changing line 11 from:
'Row ' + str(c.row) + ',Column ' + c.column + ' is ' + c.value
to:
'Row ' + str(c.row) + ',Column ' + str(c.column) + ' is ' + c.value
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#7
That has worked!! thank you. it makes sense...
Reply
#8
(Sep-23-2019, 09:22 PM)Shafla Wrote: That has worked!! thank you. it makes sense...
You are welcome! Big Grin
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#9
'Row ' + str(c.row) + ',Column ' + str(c.column) + ' is ' + c.value
With f-string it look better Wink
f'Row {c.row} ,Column {c.column} is {c.value}'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb Help using Google Sheets matheuspimenta 0 686 Dec-15-2022, 05:36 PM
Last Post: matheuspimenta
  How to loop through all excel files and sheets in folder jadelola 1 4,329 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Openpyxl-change value of cells in column based on value that currently occupies cells phillipaj1391 5 9,568 Mar-30-2022, 11:05 PM
Last Post: Pedroski55
  Run the code for some stuff it does not return me why Anldra12 3 2,797 Apr-19-2021, 02:01 PM
Last Post: Anldra12
  How can I iterate through all cells in a column (with merge cells) with openpyxl? aquerci 1 7,445 Feb-11-2021, 09:31 PM
Last Post: nilamo
  "Automate the Boring Stuff with Python" creating a path works but only for CMD promt Milos 2 2,821 Nov-28-2020, 01:08 PM
Last Post: Larz60+
  Unable to print stuff from while loop Nick1507 4 2,275 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  identical cells in 2 different excel sheets python pandas esso 0 1,598 Jul-19-2020, 07:50 PM
Last Post: esso
  Copy certain cells into new workbook certain cells Kristenl2784 4 2,446 Jul-14-2020, 07:59 PM
Last Post: Kristenl2784
  How Do I Install Stuff for Python? CopBlaster 6 3,131 May-08-2020, 12:27 PM
Last Post: hussainmujtaba

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020