用Python讀取Excel(*.xls)文件——xlrd模塊的使用

使用 xlrd 能夠很方便的讀取 excel 文件內容,而且這是個跨平臺的庫,能夠在windows,linux/unix,等平臺上面使用。

軟件可以去這個地址http://www.lexicon.net/sjmachin/xlrd.htm下載。

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)


詳細的xlrd模塊幫助在他的主頁上http://www.lexicon.net/sjmachin/xlrd.html

如果想徹底研究excel的話,這邊有講解excel格式的文檔:

http://sc.openoffice.org/excelfileformat.pdf

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章