xlrd模块方法
常用方法:
#encoding=utf-8
import sys
import xlrd
#定义excel的文件路径
file_path = '../dataconfig/case1.xls'
#通过open_workbook获得一个excel对象
data = xlrd.open_workbook(file_path)
#根据索引获得工作表
table = data.sheets()[0]
#根据索引获得工作表
table2 = data.sheet_by_index(1)
#根据工作表name获得工作表
table3 = data.sheet_by_name('Sheet2')
#返回工作表的名字的列表
print data.sheet_names()
#检查指定索引的工作表是否load完整
print data.sheet_loaded(0)
print '===========row==========='
#返回工作表的行数
print table.nrows
#返回指定行的text类型内容的列表
print table.row(rowx=1) #[text:u'Imooc-01', text:u'\u767b\u5f55', text:u'http://m.imooc.com/passport/user/login', text:u'yes', text:u'Post', text:u'no', empty:u'', empty:u'', empty:u'', text:u'user', text:u"select * from web_user where Name='mushishi'", text:u'pass']
#返回指定行的值的列表
print table.row_values(1) #[u'Imooc-01', u'\u767b\u5f55', u'http://m.imooc.com/passport/user/login', u'yes', u'Post', u'no', u'', u'', u'', u'user', u"select * from web_user where Name='mushishi'", u'pass']
print '==========column========='
#返回工作表的列数
print table.ncols
#返回指定列的内容
print table.col(1) #[text:u'\u6a21\u5757', text:u'\u767b\u5f55', text:u'\u624b\u673a\u767b\u5f55', text:u'\u5b9e\u6218list', text:u'\u8bfe\u7a0b\u4fe1\u606f', text:u'\u8bfe\u7a0b\u7c7b\u578b', text:u'\u8bfe\u7a0blist', text:u'\u83b7\u53d6\u8bfe\u7a0b\u4fe1\u606f', text:u'\u83b7\u53d6\u8bfe\u7a0b\u7ae0\u8282\u4fe1\u606f', text:u'\u6536\u85cf\u8bfe\u7a0b', text:u'\u4e0b\u5355', text:u'\u652f\u4ed8']
print table.col_values(1) #[u'\u6a21\u5757', u'\u767b\u5f55', u'\u624b\u673a\u767b\u5f55', u'\u5b9e\u6218list', u'\u8bfe\u7a0b\u4fe1\u606f', u'\u8bfe\u7a0b\u7c7b\u578b', u'\u8bfe\u7a0blist', u'\u83b7\u53d6\u8bfe\u7a0b\u4fe1\u606f', u'\u83b7\u53d6\u8bfe\u7a0b\u7ae0\u8282\u4fe1\u606f', u'\u6536\u85cf\u8bfe\u7a0b', u'\u4e0b\u5355', u'\u652f\u4ed8']
print '=====cell============'
#返回指定单元格的对象
print table.cell(1,1)
#返回指定单元格的对象类型:
# 说明:ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
print table.cell_type(1,1)
#返回指定单元格的内容
print table.cell_value(1,1)
封装excel操作方法
读取excel的代码封装
#coding:utf-8
import xlrd
from xlutils.copy import copy
from newFrameTest.dataconfig.modelConfig import ModelConfig
class ExcelOpe():
default_excel = None
# 初始化函数
# 如何构建fileName,这里用绝对路径好还是相对路径好????
def __init__(self,fileName=None,sheetNum=0):
if fileName:
self.fileName = fileName
else:
self.fileName = '../datas/case2.xls'
#sheetNum 默认给0 ,如果不是0就按实参传值
self.sheet = xlrd.open_workbook(self.fileName).sheets()[sheetNum]
# 获得行数
def get_row_num(self):
return self.sheet.nrows
# 获得列数
def get_col_num(self):
return self.sheet.ncols
# 根据行号获得整行的内容
def get_row_data(self,row_id):
if row_id:
return self.sheet.row_values(row_id)
else:
return self.sheet.row_values(0)
# 根据列号获得整列的内容
def get_col_data(self,col_id):
if col_id:
return self.sheet.col_values(col_id)
return self.sheet.col_values(0)
# 获得某个单元格的内容
def get_cell_value(self,row_id=0,col_id=0):
return self.sheet.cell_value(row_id,col_id)
# 根据用例id返回行号
def get_rowId_byCId(self,case_id):
col_num = ModelConfig().getId()
row_num = None
for i in range(1,self.get_row_num()):
if self.get_cell_value(i,col_num) == case_id:
row_num = i
return row_num
#写数据
def write_result(self,row,col,value):
read_data = xlrd.open_workbook(self.fileName)
write_data = copy(read_data)
sheet = write_data.get_sheet(1)
sheet.write(row,col,value)
write_data.save(self.fileName)