python 读取excel

来源:互联网 发布:淘宝分销平台供应商 编辑:程序博客网 时间:2024/05/01 03:14
使用 xlrd 能够很方便的读取 excel 文件内容,而且这是个跨平台的库,能够在windows,linux/unix,等平台上面使用。

软件可以去这个地址http://www.lexicon.net/sjmachin/xlrd.htm下载。

[python] view plaincopy
  1. import xlrd  
  2.    
  3. fname = "sample.xls"  
  4. bk = xlrd.open_workbook(fname)  
  5. shxrange = range(bk.nsheets)  
  6. try:  
  7.     sh = bk.sheet_by_name("Sheet1")  
  8. except:  
  9.     print "no sheet in %s named Sheet1" % fname  
  10.     return None  
  11. nrows = sh.nrows  
  12. ncols = sh.ncols  
  13. print "nrows %d, ncols %d" % (nrows,ncols)  
  14.    
  15. cell_value = sh.cell_value(1,1)  
  16. print cell_value  
  17.    
  18. row_list = []  
  19. for i in range(1,nrows):  
  20.     row_data = sh.row_values(i)  
  21.     row_list.append(row_data)  


详细的xlrd模块帮助在他的主页上http://www.lexicon.net/sjmachin/xlrd.html

如果想彻底研究excel的话,这边有讲解excel格式的文档:

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

0 0