简体   繁体   English

Python OpenPyxl:如何遍历 1 Row 的列以查找值

[英]Python OpenPyxl: How to iterate through 1 Row's columns to find a value

When using Python OpenPyxl, how would I iterate through 1 Row's columns to find a value?使用 Python OpenPyxl 时,我将如何遍历 1 Row 的列以查找值?

Here's my current (failing) attempt -- (There appears to be an error in (at least) my 2nd for loop.)这是我当前(失败)的尝试——((至少)我的第二个 for 循环中似乎有错误。)

 book = openpyxl.load_workbook(excelFile) for sheet in book.worksheets: #For each worksheet for colidx in sheet.iter_cols(sheet.min_col,sheet.max_col): #For each column in a worksheet if sheet.cell(1,colidx).value == "ValueImLookingFor": #Check each column in Row #1 for value print ("ValueImLookingFor is in cell A" + colidx)

Thanks so much for your help,非常感谢你的帮助,

CG CG

Try this:尝试这个:

from openpyxl import load_workbook
wb = load_workbook('C:/Users/viupadhy/Desktop/Results.xlsx')
ws = wb.active
count=0
while count < (ws.max_column):
    for row in ws.rows:
        if row[count].value == "CCL":
            print(row)
    count+=1

The output will be the tulpe of indexes. output 将是索引的元组。

(<Cell 'Results'.A10>, <Cell 'Results'.B10>, <Cell 'Results'.C10>, <Cell 'Results'.D10>)

You can parse the exact indexes from tuple if you want!!!如果需要,您可以从元组中解析确切的索引!!!

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

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