CODE
import openpyxl
wb = openpyxl.open('the path to the file')
wb.active = 1
ws = wb.active
for row in ws.iter_rows('B4:F4'):
for cell in row:
if cell.value == "Maria":
print(ws.cell(row=cell.row, column=2).value)
Mistake: for row in range(min_row, max_row + 1):
TypeError: 'str' object cannot be interpreted as an integer
I need search "Pavel" and print it in console
Where is the mistake?
It looks like the method iter_rows
in the library has changed, refer to this answer:
How we can use iter_rows() in Python openpyxl package?
it might seem clearer to iterate the rows/columns by integers:
import openpyxl
wb = openpyxl.open('./test-2.xlsx')
ws = wb.active
# row 4
for row in range (4, 5):
# column B ~ column F
for column in range (2, 7):
cell = ws.cell(row, column)
if cell.value == "Pavel":
print(ws.cell(row=cell.row, column=column).value)
Update (September 1, 2025):
Actually, the signature did change.
After investigating the code history, I found that the iter_rows
method signature has indeed changed between versions:
Version 2.5.14 (2019-01-23), and the parameter range_string
was marked as deprecated
:
def iter_rows(self, range_string=None, min_row=None, max_row=None, min_col=None, max_col=None, values_only=False):
Version 2.6.0 (2019-02-06):
def iter_rows(self, min_row=None, max_row=None, min_col=None, max_col=None, values_only=False):
The range_string
parameter was removed in version 2.6.0 (2019-02-06), which means code like iter_rows('B4:F4')
would no longer work.