Home >Backend Development >Python Tutorial >Python使用xlrd读取Excel格式文件的方法

Python使用xlrd读取Excel格式文件的方法

WBOY
WBOYOriginal
2016-06-06 11:21:481252browse

本文实例讲述了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程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn