2

Is there any python script to read data from excel sheet in selenium python framework?

I have written below code but i don't think its completely right

def getcell(rows,cols):

table=list()
record=list()

for x in range(rows):
    for y in range(cols):
        record.append(SheetName.cell(x,y).value)    
        table.append(record)
        record=[]
        rows=rows+1

return table;

print(table)

CPP
  • 29
  • 1
  • 5
  • I don't think that this question is related to Selenium in any way (it my may merely be indirectly related to test automation). Please edit the question and the tags accordingly. Thanks. – Arnon Axelrod Mar 25 '18 at 20:13
  • Have you try pandas ? It seems like natural in order to treat your problem ? – Jrmyy Mar 26 '18 at 17:15

1 Answers1

3

try xrld. A python module used to read ms excel sheets. Here is a simple example provided in its readme file

import xlrd
book = xlrd.open_workbook("myfile.xls")
print "The number of worksheets is", book.nsheets
print "Worksheet name(s):", book.sheet_names()
sh = book.sheet_by_index(0)
print sh.name, sh.nrows, sh.ncols
print "Cell D30 is", sh.cell_value(rowx=29, colx=3)
for rx in range(sh.nrows):
    print sh.row(rx)
And3r50n 1
  • 337
  • 1
  • 5
  • 21
  • hi And3r50n 1, it worked. I have one doubt, can we write code where in there is an excel sheet and we have to read multiple rows using tc id's.for ex: I have to read 1 row by passing an argument tc01. – CPP Mar 25 '18 at 07:04
  • yes it will. go ahead and embed the module in program . Please accept the answer and up vote it if it solved your problem – And3r50n 1 Mar 25 '18 at 07:25
  • so can you please help me to write read multiple rows using tc id's script. – CPP Mar 25 '18 at 07:43