Python Forum

Full Version: Openpyxl
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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...
There is an error in tutorial.
cells = sheet['A1','B7'] 

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