Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Openpyxl
#1
Hello, this is my first post.

A tutorial I am following tells me to type this code:

import openpyxl  
  
wb = openpyxl.load_workbook('marks.xlsx')  
  
sheet = wb.active  
#  
cells = sheet['A1','B7']  
# cells behave like range operator  
for i1,i2 in cells:  
    print("{0:8} {1:8}".format(i1.value,i2.value))  
However, I get this error:

Traceback (most recent call last):
File "/Users/marktimmermans/Desktop/untitled2/test2.py", line 66, in <module>
cells = sheet['A1', 'B7']
File "/Users/marktimmermans/.local/share/virtualenvs/marktimmermans-1t9Wp73_/lib/python3.7/site-packages/openpyxl/worksheet/worksheet.py", line 286, in __getitem__
min_col, min_row, max_col, max_row = range_boundaries(key)
File "/Users/marktimmermans/.local/share/virtualenvs/marktimmermans-1t9Wp73_/lib/python3.7/site-packages/openpyxl/utils/cell.py", line 133, in range_boundaries
m = ABSOLUTE_RE.match(range_string)
TypeError: expected string or bytes-like object

PS: here you can find the tutorial: https://www.javatpoint.com/python-openpyxl (I used a file of my own)
Reply
#2
Welcome to the forum
Well, I went through the link, and it seems that openpxyl is actually a combination of excel and python, that is, it isn't the actual python. So, if I tell anything wrong, please forgive me.
In the place where they tell you print the code, you have to import a file which has the list of marks and I think that's where you are going wrong...
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
There is an error in tutorial.
cells = sheet['A1','B7'] 

# Change to
cells = sheet['A1': 'B7']
Reply
#4
That's it, thank you so much!
Reply


Forum Jump:

User Panel Messages

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