2013年1月24日星期四

转:Python 使用 xlrd 读取 Excel格式文件

转自:http://www.pythonclub.org/python-files/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.ncolsprint "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)

没有评论:

发表评论