阅读机器人框架中的excel内容

时间:2017-06-20 17:10:47

标签: python robotframework

我是机器人框架和python的新手。我正在使用SudsLibrary处理Web服务。我想从excel文件中读取数据内容。我已经为它编写了下面的代码,但它只从文件中读取了一行。我希望它能读取文件中的所有行。

Test.robot 

*** Settings ***
Library    DataReader.py

*** Variables ***
 ${file}   ${CURDIR}${/}Book2.xls
 ${sheet}  ABC

*** Test Cases *** 
Test data provider
[Setup]   prepare data
Create Soap Client    http://test.asmx?WSDL
${ABC}    Create Wsdl Object   ABC
:FOR  ${ABC.Col1}  ${ABC.Col2}  ${ABC.Col3}  ${ABC.Col4}  ${ABC.Col5}      ${ABC.Col6}  ${ABC.Col7}   in   @{testData}
\  ${ABC.Col1}    Set Variable    ${ABC.Col1}
\  ${ABC.Col2}   Set Variable    ${ABC.Col2}
\  ${ABC.Col3}  Set Variable    ${ABC.Col3}
\  ${ABC.Col4}  Set Variable    ${ABC.Col4}
\  ${ABC.Col4} =  convert to integer     ${ABC.Col4}
\  ${ABC.Col5}  Set Variable   ${ABC.Col5}
\  ${ABC.Col6}  Set Variable     ${ABC.Col6}
\  ${ABC.Col6}=  convert to integer     ${ABC.Col6}
\  ${ABC.Col7}   Set Variable    ${ABC.Col7}
\  ${ABC.Col7}=  convert to integer     ${ABC.Col7}
\  Set Test Variable    ${ABC}
\  Call Soap Method    ABC    ${ABC}
\  ${soap_response}    Get Last Received
\  Log    ${soap_response}
\  Element Text Should Be    ${soap_response}    2.991880011689


*** Keywords ***
prepare data
${data}=   getDataFromSpreadsheet    ${file}   ${sheet}
Set Test Variable   ${testData}     ${data}

DataReader.py

import xlrd

def getDataFromSpreadsheet(fileName, sheetname) : 
workbook = xlrd.open_workbook(fileName)
worksheet = workbook.sheet_by_name(sheetname)
print worksheet
rowEndIndex = worksheet.nrows - 1
colEndIndex = worksheet.ncols - 1 
rowStartIndex = 1
colStartIndex = 0
testData = []
dataRow = []

curr_row = rowStartIndex
while curr_row <= rowEndIndex:
     cur_col = colStartIndex
     while cur_col <= colEndIndex:
         cell_type = worksheet.cell_type(curr_row, cur_col)

         value = worksheet.cell_value(curr_row, cur_col)
         dataRow.append(value)
         cur_col+=1
     curr_row += 1
     # testData.append(dataRow)
# return testData  
return dataRow

`

2 个答案:

答案 0 :(得分:0)

要从csv逐行读取数据,我在python代码下面使用。

csvLibrary.py

        id good bad none
id type
1  a     2    2   0    0
1  b     1    1   0    0
1  c     1    0   1    0
2  a     1    1   0    0
2  b     1    0   1    0
3  a     1    0   0    1

答案 1 :(得分:0)

以下是从Excel文件中读取值的示例代码 -

Open Excel    ${CURDIR}/${EXCEL_FILE_NAME}

${strColCount} =  Get Column Count  ${EXCEL_SHEET_NAME}

Log To Console  \nCols are => ${strColCount}

${strRowCount} =  Get Row Count ${EXCEL_SHEET_NAME}

Log To Console  \nRows are=> ${strRowCount}

Set Test Variable   ${ROW_ID}   3

:FOR    ${colIndex}    IN RANGE    1    ${strColCount}
    \   ${strTempColValue}  Read Cell Data By Coordinates   ${EXCEL_SHEET_NAME} ${colIndex} ${ROW_ID}

变量strTempColValue现在具有所需列索引和值的值。给定的行。在这个例子中,我们将行设为3。

http://navinet.github.io/robotframework-excellibrary/ExcelLibrary-KeywordDocumentation.html