Python Forum

Full Version: Getting Cells from the Sheets "automate the boring stuff"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
It looks like an error, int(c.row) should probably be str(c.row)
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
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.
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
(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,
That has worked!! thank you. it makes sense...
(Sep-23-2019, 09:22 PM)Shafla Wrote: [ -> ]That has worked!! thank you. it makes sense...
You are welcome! Big Grin
'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}'