On Jul 24, 5:15*pm, Girish <girish....@gmail.comwrote in
comp.lang.python:
Hello,
Does any one have a sample piece of code to search for a keyword in
Excel sheet? if so plz post it..
8<--- xlkwsearch.py
import xlrd, sys, glob
def xlkwsearch(fname, query):
book = xlrd.open_workbook(fname)
for sheet in book.sheets():
for rowx in xrange(sheet.nrows):
for colx in xrange(sheet.ncols):
cell = sheet.cell(rowx, colx)
if cell.ctype == xlrd.XL_CELL_TEXT and query in
cell.value:
yield fname, sheet.name, rowx, colx, cell.value
if __name__ == '__main__':
for fname in glob.glob(sys.argv[1]):
for result in xlkwsearch(fname, sys.argv[2]):
print result
8<---
Sample output:
D:\junk>python xlkwsearch.py *search*.xls hello
('search_demo.xls', u'Sheet1', 0, 0, u'hello')
('search_demo.xls', u'Sheet1', 2, 6, u'hello world')
D:\junk>python xlkwsearch.py *search*.xls world
('search_demo.xls', u'Sheet1', 1, 1, u'world')
('search_demo.xls', u'Sheet1', 2, 6, u'hello world')
('search_demo.xls', u'2nd Sheet', 0, 0, u'underworld')
('search_demo.xls', u'2nd Sheet', 0, 2, u'worldly')