python search text in Excel workbook

created the script to search text in whole excel workbook, include all work sheets!

import sys
import openpyxl

wb = openpyxl.load_workbook(sys.argv[1])
worksheets = wb.get_sheet_names()
print(worksheets)
def wordfinder(ws, searchString):
    for i in range(1, ws.max_row + 1):
        for j in range(1, ws.max_column + 1):
            if str(ws.cell(i,j).value).find(searchString) != -1:
                print("found")
                print(ws.cell(i,j))   
                
for sheetname in worksheets:
    ws = wb.get_sheet_by_name(sheetname)
    #print(ws.title)
    wordfinder(ws, sys.argv[2])
C:\>python search_sheets.py hello.xlsx asdfa
['Impacted servers', 'Sheet1']
found
<Cell 'Sheet1'.A1>

Leave a Reply

Your email address will not be published. Required fields are marked *