xlrd是Python中经常使用于解析excel文件的模块,提供了非常简单易用的API来完成相干操作.
xlrd的经常使用使用方法以下:
import xlrd
book = xlrd.open_workbook("speechs.xlsx","utf8")
sheet = book.sheet_by_name(u'机器地址') # 通过名字来查找对应的sheet
rows = sheet.nrows # 读取行数
for row in xrange(row):
row_value = sheet.row_values(row) # 读取1行的数据
ip = str(row_value[1]) # 从数组中按序号便可获得对应的cell的值.
另外,xlrd的其他方法以下,
sheet = book.sheets()[0] # 通过sheet的索引顺序来获得
sheet = book.sheet_by_index(0)
ncols = sheet.ncols # 获得列数
sheet.col_values(col) # 获得1列数据
cell = sheet.cell(0,0).value # 通过cell位置获得cell的值
cell = sheet.row[0][0].value # 通过行列索引来获得cell的值
通过put_cell来向cell中写入值,sheet.put_cell(row,col,ctype,value,xf),
如 sheet.put_cell(0,1,'cell_value',0)
ctype表示类型,0-empty,1-string,2-number,3-date,4-boolean,5-error.