python操作excel表格文件--使用xlrd模块

时间:2023-03-09 17:42:23
python操作excel表格文件--使用xlrd模块

原文:

http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html

引言:

  实际工作中,可能很多情况下都会用到excel表格,像如果不需要很正规的用例工具来管理用例的话,大多公司选择直接用excel来管理用例;包括api自动化测试在设计接口的测试用例时,一般会先将接口的url、方法、参数、报文、接口描述等用excel维护起来,然后再从excel表格中读取这些接口信息;

实际的使用步骤如下:

1.安装xlrd模块:

  很简单吧,pip install xlrd即可;

2.导入模块

  import xlrd

3.打开Excel文件读取数据,创建文件对象赋值给workfile

  workfile = xlrd.open_workbook('excelFile.xlsx')

4.获得一个工作表(sheet),创建一个表格对象赋值给变量table,三种方法:

  table = workfile.sheets()[0]          #通过索引顺序获取

  table = workfile.sheet_by_index(0) #通过索引顺序获取

  table = workfile.sheet_by_name(u'Sheet1')#通过名称获取

5.获取整行和整列的值(列表)

  table.row_values(0)  #获取第一行的值,以列表形式返回

  table.col_values(0)  #获取第一列的值,以列表形式返回

6.获取行数和列数

  nrows = table.nrows

   ncols = table.ncols
7.根据行数编列所有行的数据
  for i in range(nrows):
    print(table.row_values(i))
8.获取指定单元格的数据,注意行和列的索引值都是从0开始
  cell_A1 = table.cell(0,0).value  #表格中A1位置的数据
  cell_C4 = table.cell(2,3).value  #表格中C4位置的数据
9.使用行列索引确定指定单元格的数据
  cell_A1 = table.row(0)[0].value  #表格中第1行第1个位置
  cell_A2 = table.col(1)[0].value  #表格中第2列第一个位置
10.简单的写入,table.put_cell()
  help一下table.put_cell(),可以了解到put_cell()方法有5个参数:rowx, colx, ctype, value, xf_index
  (1) rowx    # 要写的行索引
  (2) colx    # 要写的列索引
  (3) ctype    # 要写的值的类型: 0 empty, 1 string,  2 number,  3 date,  4 boolean,  5 error
  (4) value    # 要写入的值的数据
  (5) xf_index  # 扩展的格式化,默认是0
>>> table.put_cell(, , , )
>>> table.cell(, ).value
'python'
>>> 

11.不需要关闭文件对象,workfile文件对象没有close()方法

12.xlrd应用代码实例

# -*- coding: utf- -*-
import  xdrlib ,sys
import xlrd
def open_excel(file= 'file.xls'):
    try:
        data = xlrd.open_workbook(file)
        return data
    except Exception,e:
        print str(e)
#根据索引获取Excel表格中的数据   参数:file:Excel文件路径     colnameindex:表头列名所在行的所以  ,by_index:表的索引
def excel_table_byindex(file= ,by_index=):
    data = open_excel(file)
    table = data.sheets()[by_index]
    nrows = table.nrows #行数
    ncols = table.ncols #列数
    colnames =  table.row_values(colnameindex) #某一行数据
    list =[]
    ,nrows):

         row = table.row_values(rownum)
         if row:
             app = {}
             for i in range(len(colnames)):
                app[colnames[i]] = row[i]
             list.append(app)
    return list

#根据名称获取Excel表格中的数据   参数:file:Excel文件路径     colnameindex:表头列名所在行的所以  ,by_name:Sheet1名称
def excel_table_byname(file= ,by_name=u'Sheet1'):
    data = open_excel(file)
    table = data.sheet_by_name(by_name)
    nrows = table.nrows #行数
    colnames =  table.row_values(colnameindex) #某一行数据
    list =[]
    ,nrows):
         row = table.row_values(rownum)
         if row:
             app = {}
             for i in range(len(colnames)):
                app[colnames[i]] = row[i]
             list.append(app)
    return list

def main():
   tables = excel_table_byindex()
   for row in tables:
       print row

   tables = excel_table_byname()
   for row in tables:
       print row

if __name__=="__main__":
    main()