简体   繁体   中英

How search data in excel with openpyxl?

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM