Searching for string in cell using openpyxl

Viewed 23

I am new to python and I have an excel sheet with ip addresses on column C and the site name on column D. I'm trying to search for the site name through the rows in column D on the sheets in the workbook. My end goal is to print the site on the cell on column D and its ip addresses on column c adjacent to it and 4 rows down, but currently I can't output the cell on column d. It says it cannot find the string.

Any help is appreciated

from openpyxl import Workbook, load_workbook

wb = load_workbook('C:/Users/bxd1/Downloads/IP Addresses.xlsx')

ws = wb.activesns = wb.sheetnames

search_word = 'site a'

rows = ws.iter_rows(min_row=1, min_col=3, values_only=True)    

for cell in rows:

    if search_word in cell:

       print(cell)

    else:

       print(search_word + ' not in cell')

Output: site a not in cell site a not in cell site a not in cell site a not in cell site a not in cell site a not in cell

0 Answers
Related