Robot Framework自动化测试----自定义读写xlsx格式的excel表格库

前言:

前面费了九牛二虎之力安装上了 robotframework-excellibrary库(见Robot Framework自动化测试----05使用ExcelLibrary库实现数据驱动_测试媛-CSDN博客),但是呢只能读写xls格式的excel表格,目前我们使用的系统上大多数就是xlsx格式的excel,在网上搜索了多久发现没有现成的RF读写xlsx的库,来就用python自己来写一个吧:

1)写之前先来选择一个模块吧,python中能读写excel的模块非常多,经过对比选择了openpyxl,openpyxl相关的方法如下:

(1)wb=openpyxl.load_workbook(excelpath)  加载一个路径为excelpath的excel表格;

(2)ws=wb[sheet名称]  获取一个名称为'sheet名称'的sheet页

(3)获取指定行的数据:

                for cell_object in list(ws.rows)[1]:  # 第一行的值
                    print(cell_object.value)

(4)获取指定列的数据:

                for cell_object in list(ws.columns)[1]: #第一列的数据
                    print(cell_object.value)

   (5)获取一个区域的数据:ws['A1':'C2']

(6)获取当前工作表中有数据的行和列:ws.max_row(行) ws.max_column(列)

(7)获取一个单元格中的内容:ws['A1'].value   ws.cell(row=i,column=j).value

(8)写入:ws['A1']=text

2)上代码:

rwexcelfile.py
__version__='1.0'

from robot.api import logger
import os
import openpyxl

class Read_Write_excelfile(object):

    def readrowdata(self,excelpath,sheetname,rownum):# 读取某一行的数据
        if not os.path.exists(excelpath):
            print('xlsx文件不存在,请检查 !!!')
        else:
            wb=openpyxl.load_workbook(excelpath)
            if sheetname not in wb.sheetnames:
                print('名为'+sheetname+'的文sheet页不存在,请检查 !!!')
            else:
                sheet=wb[sheetname]
                rowlist=[]
                for cell_object in list(sheet.rows)[rownum-1]:  # 第一列
                    #print(cell_object.value)
                    if cell_object.value!=None:
                        rowlist.append(cell_object.value)
                return rowlist


    def readcolmundata(self,excelpath,sheetname,columnum):  #读取某一列的数据
        if not os.path.exists(excelpath):
            print('xlsx文件不存在,请检查 !!!')
        else:
            wb = openpyxl.load_workbook(excelpath)
            if sheetname not in wb.sheetnames:
                print('名为' + sheetname + '的文sheet页不存在,请检查 !!!')
            else:
                sheet=wb[sheetname]
                columnumlist = []
                for cell_object in list(sheet.columns)[columnum-1]:
                    #print(cell_object.value)
                    if cell_object.value!=None:
                        columnumlist.append(cell_object.value)
                return columnumlist


    def readcelldata(self,excelpath,sheetname,cellname):  #读取某一个单元格的数据,sheet['A1'].value或者 sheet.cell(row=i,column=j).value
        if not os.path.exists(excelpath):
            print('xlsx文件不存在,请检查 !!!')
        else:
            wb = openpyxl.load_workbook(excelpath)
            if sheetname not in wb.sheetnames:
                print('名为' + sheetname + '的文sheet页不存在,请检查 !!!')
            else:
                wb=openpyxl.load_workbook(excelpath)
                sheet=wb[sheetname]   #sheet.max_row  excel表格的最大行,sheet.max_column excel表格的最大列
                #print(sheet[cellname].value)
                return sheet[cellname].value


    def writecelldata(self,excelpath,sheetname,cellname,text):
        if not os.path.exists(excelpath):
            print('xlsx文件不存在,请检查 !!!')
        else:
            wb = openpyxl.load_workbook(excelpath)
            if sheetname not in wb.sheetnames:
                print('名为' + sheetname + '的文sheet页不存在,请检查 !!!')
            else:
                wb=openpyxl.load_workbook(excelpath)
                sheet=wb[sheetname]   #sheet.max_row  excel表格的最大行,sheet.max_column excel表格的最大列
                sheet[cellname]=text
                wb.save(excelpath)


if __name__=="__main__":
    rdexcel=Read_Write_excelfile()
    print(rdexcel.readcolmundata("E:\\script\\Test\\test_bimtiles\\data.xlsx","data",1))  #读取第一行的内容

__init__.py

from .rwexcelfile import Read_Write_excelfile

__version__='1.0'

class OprateExcelLibrary(Read_Write_excelfile):
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

  在python安装目录下\Lib\site-packages\新建一个OprateExcelLibrary 文件夹,把    rwexcelfile.py和__init__.py放进去,然后在rf加载到Library中

rf使用:

                    

 最后会次输出指定excel表格中第一列中的内容

3)使用:

(1)导库,定义关键字:

(2)在case中使用:

 

 


                
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值