def read_from_excel(filepath, sheetname, columnname, rowno):
sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
for i in range(sheetobj.ncols):
if sheetobj.cell_value(0, i) == columnname:
return sheetobj.cell_value(rowno, i)
Mayur wrote:
> Hi all,
> I need to read data from excel sheet using
> filepath,sheetname,columnname,rowno for this i had used xlrd package &
> written code in python
> def read_from_excel(filepath, sheetname, columnname, rowno):
> sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
> for i in range(sheetobj.ncols):
> if sheetobj.cell_value(0, i) == columnname:
> return sheetobj.cell_value(rowno, i)
> Mayur wrote:
> > Hi all,
> > I need to read data from excel sheet using
> > filepath,sheetname,columnname,rowno for this i had used xlrd package &
> > written code in python
def read_from_excel(filepath, sheetname, columnname, rowno):
sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
for i in range(sheetobj.ncols):
if sheetobj.cell_value(0, i) == columnname:
return sheetobj.cell_value(rowno, i)
Mayur wrote:
> Hi all,
> I need to read data from excel sheet using
> filepath,sheetname,columnname,rowno for this i had used xlrd
package &
> written code in python
>
> def read_from_excel(self,filepath,sheetname,columnname,rowno):
> book=xlrd.open_workbook(filepath)
> sheetobj=book.sheet_by_name(sheetname)
> colx=sheetobj.ncols
> temp=0
> while colx:
> if sheetobj.cell_value(1,temp)
> ==columnname:
> return sheetobj.cell_value(rowno,
> temp)
> else:
> colx=colx-1
> temp=temp+1
>
> but this function returning value none.
> Please help me any one can suggest the changes in code or will
send me
> the new code
>
> Thanks,
> Mayur
>
>
> >
>
Hi georgi,
I had changed the code i have to give range in a for loop but
it is still returning me None.
I thought the numbering of column in excel sheet is from 1 instead of 0 so
i changed it but it is still returning none the code with very few
modification is
sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
colx=sheetobj.ncols
for i in range(1,colx):
if sheetobj.cell_value(1, i) == columnname:
return sheetobj.cell_value(rowno, i)
what problem do you think I am facing.
Thanks,
Mayur
2009/7/7 Георги Георгиев <georgi_georg...@directservices.bg>
> the attached script works here with the attached .xls - please try it
> MAYUR BONGIRWAR wrote:
> Thanks georgi
> But it is generating error TypeError: list indices must be integers,
> not unicode
> 2009/7/7 Георги Георгиев <georgi_georg...@directservices.bg>
>> hey :-)
>> what about
>> def read_from_excel(filepath, sheetname, columnname, rowno):
>> sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
>> for i in range(sheetobj.ncols):
>> if sheetobj.cell_value(0, i) == columnname:
>> return sheetobj.cell_value(rowno, i)
>> Mayur wrote:
>> > Hi all,
>> > I need to read data from excel sheet using
>> > filepath,sheetname,columnname,rowno for this i had used xlrd package &
>> > written code in python
could be off by one (did you tried the script with 0 instead of 1 in
both places?), could be different encoding in .xls and script and
probably other things :-)
can you send me sample script and .xls so i can test it?
MAYUR BONGIRWAR wrote:
Hi georgi,
I had changed the code i have to give range in a for loop
but it is still returning me None.
I thought the numbering of column in excel sheet is from 1 instead of
0 so i changed it but it is still returning none the code with very few
modification is
def read_from_excel(self,filepath,sheetname,columnname,rowno):
sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
colx=sheetobj.ncols
for i in range(1,colx):
if sheetobj.cell_value(1, i) == columnname:
return sheetobj.cell_value(rowno, i)
def read_from_excel(filepath, sheetname, columnname, rowno):
sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
for i in range(sheetobj.ncols):
if sheetobj.cell_value(0, i) == columnname:
return sheetobj.cell_value(rowno, i)
Mayur wrote:
> Hi all,
> I need to read data from excel sheet using
> filepath,sheetname,columnname,rowno for this i had used xlrd
package &
> written code in python
>
> def read_from_excel(self,filepath,sheetname,columnname,rowno):
> book=xlrd.open_workbook(filepath)
> sheetobj=book.sheet_by_name(sheetname)
> colx=sheetobj.ncols
> temp=0
> while colx:
> if sheetobj.cell_value(1,temp)
> ==columnname:
> return sheetobj.cell_value(rowno,
> temp)
> else:
> colx=colx-1
> temp=temp+1
>
> but this function returning value none.
> Please help me any one can suggest the changes in code or will
send me
> the new code
>
> Thanks,
> Mayur
>
>
> >
>
The problem is solved now. I am using that function in robot framework. The
robot framework sends the parameter as string so I used type casting the
problem is solved now.
Thanks for your valuable suggestions,
Mayur
2009/7/7 Георги Георгиев <georgi_georg...@directservices.bg>
> could be off by one (did you tried the script with 0 instead of 1 in both
> places?), could be different encoding in .xls and script and probably other
> things :-)
> can you send me sample script and .xls so i can test it?
> MAYUR BONGIRWAR wrote:
> Hi georgi,
> I had changed the code i have to give range in a for loop but
> it is still returning me None.
> I thought the numbering of column in excel sheet is from 1 instead of 0 so
> i changed it but it is still returning none the code with very few
> modification is
> sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
> colx=sheetobj.ncols
> for i in range(1,colx):
> if sheetobj.cell_value(1, i) == columnname:
> return sheetobj.cell_value(rowno, i)
> what problem do you think I am facing.
> Thanks,
> Mayur
> 2009/7/7 Георги Георгиев <georgi_georg...@directservices.bg>
>> hmmm strange...
>> the attached script works here with the attached .xls - please try it
>> MAYUR BONGIRWAR wrote:
>> Thanks georgi
>> But it is generating error TypeError: list indices must be integers,
>> not unicode
>> 2009/7/7 Георги Георгиев <georgi_georg...@directservices.bg>
>>> hey :-)
>>> what about
>>> def read_from_excel(filepath, sheetname, columnname, rowno):
>>> sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
>>> for i in range(sheetobj.ncols):
>>> if sheetobj.cell_value(0, i) == columnname:
>>> return sheetobj.cell_value(rowno, i)
>>> Mayur wrote:
>>> > Hi all,
>>> > I need to read data from excel sheet using
>>> > filepath,sheetname,columnname,rowno for this i had used xlrd package &
>>> > written code in python
i'm glad to hear that - good luck with your project :-)
MAYUR BONGIRWAR wrote:
Hi Georgie,
The problem is solved now. I am using that function in robot
framework. The robot framework sends the parameter as string so I used
type casting the problem is solved now.
could be off by one (did you
tried the script with 0 instead of 1 in
both places?), could be different encoding in .xls and script and
probably other things :-)
can you send me sample script and .xls so i can test it?
MAYUR BONGIRWAR wrote:
Hi georgi,
I had changed the code i have to give range in a for loop
but it is still returning me None.
I thought the numbering of column in excel sheet is from 1 instead of
0 so i changed it but it is still returning none the code with very few
modification is
def read_from_excel(self,filepath,sheetname,columnname,rowno):
sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
colx=sheetobj.ncols
for i in range(1,colx):
if sheetobj.cell_value(1, i) == columnname:
return sheetobj.cell_value(rowno, i)
def read_from_excel(filepath, sheetname, columnname, rowno):
sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
for i in range(sheetobj.ncols):
if sheetobj.cell_value(0, i) == columnname:
return sheetobj.cell_value(rowno, i)
Mayur wrote:
> Hi all,
> I need to read data from excel sheet using
> filepath,sheetname,columnname,rowno for this i had used xlrd
package &
> written code in python
>
> def read_from_excel(self,filepath,sheetname,columnname,rowno):
> book=xlrd.open_workbook(filepath)
> sheetobj=book.sheet_by_name(sheetname)
> colx=sheetobj.ncols
> temp=0
> while colx:
> if sheetobj.cell_value(1,temp)
> ==columnname:
> return sheetobj.cell_value(rowno,
> temp)
> else:
> colx=colx-1
> temp=temp+1
>
> but this function returning value none.
> Please help me any one can suggest the changes in code or will
send me
> the new code
>
> Thanks,
> Mayur
>
>
> >
>
> Hi georgi, > I had changed the code i have to give range in a for loop > but it is still returning me None. > I thought the numbering of column in excel sheet is from 1 instead of 0 > so i changed it but it is still returning none the code with very few > modification is
Hi, Mayur,
Your original problem was that you didn't have a return statement that explicitly returned the value that you found; control flow fell off the bottom of your function, so the default, None, was returned.
Second problem (unicode as a list index): It would help quite a lot if you were to show the exact script that you ran, together with the full traceback and error message.
Note that rowno should be an int; passing in a unicode string as the rowno arg would cause that error message.
Current problem: looks like none of the column headings match your input, so it is falling of the end of the function, and returning None. [Please get used to typing None, not none].
xlrd is intended for Python programmers; column indexes and row indexes start at zero.
I've added some debug print statements and some corrections into your code below:
[untested]
DEBUG = True
def read_from_excel(self, filepath, sheetname, columnname, rowno): # what is `self` for? This seems like a very unlikely class method. IF DEBUG: print "columname: %r, rowno: %r" % (columnname, rowno) sheetobj = xlrd.open_workbook(filepath).sheet_by_name(sheetname) # colx=sheetobj.ncols # use meaningful names; colx means column_index for colx in range(sheetobj.ncols): value = sheetobj.cell_value(0, colx) if DEBUG: print "rowx=0 colx=%d value=%r" % (colx, value) if value == columnname: return sheetobj.cell_value(rowno, colx) raise ValueError("%s not found in first row" % columnname)
# stripped down, somewhat more Pythonic version:
def read_from_excel(filepath, sheetname, columnname, rowno): book = xlrd.open_workbook(filepath, on_demand=True) # using on_demand=True so that only 1 sheet is loaded sheet = book.sheet_by_name(sheetname) headings = sheetobj.row_values(0) try: colx = headings.index(columnname) except IndexError: raise ValueError("%s not found in first row" % columnname) return sheetobj.cell_value(rowno, colx)