Posts: 25
Threads: 9
Joined: Apr 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
Posts: 4,786
Threads: 76
Joined: Jan 2018
It looks like an error, int(c.row) should probably be str(c.row)
Posts: 25
Threads: 9
Joined: Apr 2019
Sep-23-2019, 08:55 PM
(This post was last modified: Sep-23-2019, 08:58 PM by Shafla.)
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
Posts: 4,786
Threads: 76
Joined: Jan 2018
Sep-23-2019, 09:00 PM
(This post was last modified: Sep-23-2019, 09:01 PM by Gribouillis.)
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.
Posts: 25
Threads: 9
Joined: Apr 2019
Sep-23-2019, 09:02 PM
(This post was last modified: Sep-23-2019, 09:03 PM by Shafla.)
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
Posts: 212
Threads: 25
Joined: Aug 2019
Sep-23-2019, 09:18 PM
(This post was last modified: Sep-23-2019, 09:19 PM by newbieAuggie2019.)
(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
Posts: 25
Threads: 9
Joined: Apr 2019
That has worked!! thank you. it makes sense...
Posts: 212
Threads: 25
Joined: Aug 2019
(Sep-23-2019, 09:22 PM)Shafla Wrote: That has worked!! thank you. it makes sense... You are welcome!
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
Posts: 7,315
Threads: 123
Joined: Sep 2016
'Row ' + str(c.row) + ',Column ' + str(c.column) + ' is ' + c.value With f-string it look better
f'Row {c.row} ,Column {c.column} is {c.value}'
|