Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching the string and getting the row and column value

Tags:

python

excel

xlrd

How to search the particular string in the excel sheet and getting the row and column value of that particular string in the excel sheet using xlrd in python? Can any one help me please?

import xlrd
workbook = xlrd.open_workbook("1234.xls")
worksheet = workbook.sheet_by_name('firstpage')

only this much i tried

like image 787
query Avatar asked Aug 19 '15 08:08

query


People also ask

How do you search a string in Excel and return a value?

The FIND function in Excel is used to return the position of a specific character or substring within a text string. The first 2 arguments are required, the last one is optional. Find_text - the character or substring you want to find. Within_text - the text string to be searched within.

How do I find the values of a row and column in Excel?

=INDEX() returns the value of a cell in a table based on the column and row number. =MATCH() returns the position of a cell in a row or column. Combined, the two formulas can look up and return the value of a cell in a table based on vertical and horizontal criteria.

How do I search for a text string in a column in Excel?

Find cells that contain textOn the Home tab, in the Editing group, click Find & Select, and then click Find. In the Find what box, enter the text—or numbers—that you need to find. Or, choose a recent search from the Find what drop-down box. Note: You can use wildcard characters in your search criteria.


2 Answers

I think this is what you are looking for , Here you go,

from xlrd import open_workbook
book = open_workbook(workbookName)
for sheet in book.sheets():
    for rowidx in range(sheet.nrows):
        row = sheet.row(rowidx)
        for colidx, cell in enumerate(row):
            if cell.value == "particularString" :
                print sheet.name
                print colidx
                print rowidx

best,

like image 191
Sujay Narayanan Avatar answered Oct 04 '22 21:10

Sujay Narayanan


Say you're searching for a string s:

for row in range(sh.nrows):
    for column in range(sh.ncols):
        if s == sh.cell(row, column).value:  
            return (row, column)
like image 26
frogbandit Avatar answered Oct 04 '22 21:10

frogbandit