简体   繁体   English

如何使用 openpyxl 搜索 excel 中的数据?

[英]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):错误:对于范围内的行(min_row,max_row + 1):

TypeError: 'str' object cannot be interpreted as an integer TypeError: 'str' object 不能解释为 integer

Excel table Excel表

I need search "Pavel" and print it in console我需要搜索“Pavel”并在控制台中打印

Where is the mistake?错误在哪里?

It looks like the method iter_rows in the library has changed, refer to this answer:看起来库中的方法iter_rows已更改,请参阅此答案:

How we can use iter_rows() in Python openpyxl package? 我们如何在 Python openpyxl package 中使用 iter_rows()?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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