0

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

Excel table

I need search "Pavel" and print it in console

Where is the mistake?

1 Answers1

0

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)

cg-zhou
  • 528
  • 3
  • 6