By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,611 Members | 870 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,611 IT Pros & Developers. It's quick & easy.

EXCEL API

P: n/a
Hello,

Does any one have a sample piece of code to search for a keyword in
Excel sheet? if so plz post it..

Thanks,
Girish..
Jul 24 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
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')
Jul 24 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.