EXCEL API

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Girish

    EXCEL API

    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..
  • John Machin

    #2
    Re: EXCEL API

    On Jul 24, 5:15 pm, Girish <girish....@gma il.comwrote in
    comp.lang.pytho n:
    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(fnam e, query):
    book = xlrd.open_workb ook(fname)
    for sheet in book.sheets():
    for rowx in xrange(sheet.nr ows):
    for colx in xrange(sheet.nc ols):
    cell = sheet.cell(rowx , colx)
    if cell.ctype == xlrd.XL_CELL_TE XT and query in
    cell.value:
    yield fname, sheet.name, rowx, colx, cell.value
    if __name__ == '__main__':
    for fname in glob.glob(sys.a rgv[1]):
    for result in xlkwsearch(fnam e, sys.argv[2]):
    print result
    8<---
    Sample output:

    D:\junk>python xlkwsearch.py *search*.xls hello
    ('search_demo.x ls', u'Sheet1', 0, 0, u'hello')
    ('search_demo.x ls', u'Sheet1', 2, 6, u'hello world')

    D:\junk>python xlkwsearch.py *search*.xls world
    ('search_demo.x ls', u'Sheet1', 1, 1, u'world')
    ('search_demo.x ls', u'Sheet1', 2, 6, u'hello world')
    ('search_demo.x ls', u'2nd Sheet', 0, 0, u'underworld')
    ('search_demo.x ls', u'2nd Sheet', 0, 2, u'worldly')

    Comment

    Working...