python使用xlrd读取excel格式文件的方法

本文实例讲述了python使用xlrd读取excel格式文件的方法。分享给大家供大家参考。具体如下:

使用xlrd能够很方便的读取excel文件内容,而且这是个跨平台的库,能够在windows,linux/unix,等平台上面使用,代码如下:

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)

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论