When using Python OpenPyxl, how would I iterate through 1 Row's columns to find a value?
Here's my current (failing) attempt -- (There appears to be an error in (at least) my 2nd for loop.)
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
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.
(<Cell 'Results'.A10>, <Cell 'Results'.B10>, <Cell 'Results'.C10>, <Cell 'Results'.D10>)
You can parse the exact indexes from tuple if you want!!!
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.