【问题标题】:Robot Framework with Excel Library erroring: "local variable 'my_sheet_index' referenced before assignment"带有 Excel 库错误的机器人框架:“分配前引用了局部变量 'my_sheet_index'”
【发布时间】:2017-08-21 12:46:17
【问题描述】:

我的 ExcelLibrary 概念证明存在问题。当我尝试将一些数据保存到新的 Excel 文件中时,它会在 Put Number To Cell sheetname=${mySheetName} column=1 row=1 value=90 上返回错误 UnboundLocalError: local variable 'my_sheet_index' referenced before assignment

您知道我可以做些什么来防止这种情况发生吗?这是我的简单测试文件:

*** Settings ***
Library             ExcelLibrary
Library             Collections

*** Variables ***
${Excel_File_Path}   C:\\Python27\\ExcelRobotTest\\
${mySheetName}         UserImport

*** Test Cases ***
Excel Test
    Lubos Test

*** Keywords ***
Lubos Test
    Create Excel Workbook    newsheetname=${mySheetName}
    Put Number To Cell   sheetname=${mySheetName}    column=1    row=1    value=90
    Save Excel Current Directory    filename=MyNewExcel.xls

【问题讨论】:

  • 我已经设法解决了这个错误UnboundLocalError: local variable 'my_sheet_index' referenced before assignment,但我现在在添加数字时遇到了IndexError: list index out of range...如果你想绕过第一个错误,我可以给你我当前的代码但我仍在尝试解决第二个问题:(
  • 随时分享@Goralight

标签: python excel robotframework


【解决方案1】:
*** Settings ***
Library             ExcelLibrary

*** Variables ***
${Path}   C:\\Python27\\ExcelRobotTest\\
${name}    Test.xls

*** Test Cases ***
Excel Test
    Create

*** Keywords ***
Create
    Create Excel Workbook    ${name}
    Open Excel    ${path}${name}
    Put String To Cell    ${name}    1    1    90
    Save Excel        ${Path}${name}

这可以解决您的第一个错误

UnboundLocalError:赋值前引用了局部变量“my_sheet_index”

我使用了Put Number To Cell,这导致了

IndexError: 列表索引超出范围

所以我使用了Put String To Cell,现在这会导致

IOError: [Errno 13] Permission denied: u'C:\Python27\ExcelRobotTest\Test.xls'

所以我认为我发布的当前代码是您作为我的工作计算机的最佳选择,因为一些有趣的政策阻止我通过机器人保存/更改文件......但我真的很难过:(如果我能做到,我会更新更多来解决这个问题。

考虑到它在抱怨烫发——它一定是写好了但不能保存。

EDIT1:快速切换策略后,它现在抛出了一个

IOError: [Errno 22] 无效模式 ('w+b') 或文件名:u'C:\Python27\ExcelRobotTest\Test.xls'

我不知道这是什么......

【讨论】:

    猜你喜欢
    • 2018-11-14
    • 2020-09-22
    • 1970-01-01
    • 2018-07-16
    • 2014-08-20
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多