Python USES XLRD to read files in Excel format

  • 2020-04-02 14:37:19
  • OfStack

This article illustrates how Python USES XLRD to read files in Excel format. Share with you for your reference. The details are as follows:

XLRD is a cross-platform library that can be used on Windows, Linux/Unix and other platforms. The code is as follows:


import xlrd
fname = "sample.xls"
bk = xlrd.open_workbook(fname)
shxrange = range(bk.nsheets)
try:
  sh = bk.sheet_by_name("Sheet1")
except:
  print "no sheet in %s named Sheet1" % fname
  return None
nrows = sh.nrows
ncols = sh.ncols
print "nrows %d, ncols %d" % (nrows,ncols)
cell_value = sh.cell_value(1,1)
print cell_value
row_list = []
for i in range(1,nrows):
  row_data = sh.row_values(i)
  row_list.append(row_data)

I hope this article has helped you with your Python programming.


Related articles: