Python Forum
Getting Cells from the Sheets "automate the boring stuff" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Getting Cells from the Sheets "automate the boring stuff" (/thread-21302.html)



Getting Cells from the Sheets "automate the boring stuff" - Shafla - Sep-23-2019

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



RE: Getting Cells from the Sheets "automate the boring stuff" - Gribouillis - Sep-23-2019

It looks like an error, int(c.row) should probably be str(c.row)


RE: Getting Cells from the Sheets "automate the boring stuff" - Shafla - Sep-23-2019

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


RE: Getting Cells from the Sheets "automate the boring stuff" - Gribouillis - Sep-23-2019

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.


RE: Getting Cells from the Sheets "automate the boring stuff" - Shafla - Sep-23-2019

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



RE: Getting Cells from the Sheets "automate the boring stuff" - newbieAuggie2019 - Sep-23-2019

(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,


RE: Getting Cells from the Sheets "automate the boring stuff" - Shafla - Sep-23-2019

That has worked!! thank you. it makes sense...


RE: Getting Cells from the Sheets "automate the boring stuff" - newbieAuggie2019 - Sep-23-2019

(Sep-23-2019, 09:22 PM)Shafla Wrote: That has worked!! thank you. it makes sense...
You are welcome! Big Grin


RE: Getting Cells from the Sheets "automate the boring stuff" - snippsat - Sep-24-2019

'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}'