1

I can iterate through cells in rows to check for cells containing a certain value. Now I'm trying to check if the row also contains second value.

target = input("Input cell value: ")
target2 = input("Input second cell value ")
wb = openpyxl.load_workbook("file.xlsx")
ws = wb.active
for ws in wb.worksheets:
    for row in ws.iter_rows():
        for cell in row:
            if cell.value == target: 
            # I'd also like to check if row contains second target
                print(cell.value)
GTA.sprx
  • 817
  • 1
  • 8
  • 24

1 Answers1

1

I'm still not sure if I really understand the question but the best way to approach this is to use sets to compare all values at once.

targets = {target, target2}
for idx, row in enumerate(ws.iter_rows(values_only=True),1):
    if set(row) & targets:
        print(f"Row {idx} contains all the values")
Charlie Clark
  • 18,477
  • 4
  • 49
  • 55