我一直在尝试将数据写入到xls工作表中,但没有成功地保存数据。
我收到以下错误: IndexError:列表索引超出范围。大多数搜索"Put Number To Cell“的结果是”创建您自己的python关键字“。这是我的代码。它检索地址的经度/纬度并将其保存到文件中。感谢任何人的帮助!
Open and Read the Excel Record
# Open the file
Open Excel ${EXCEL_FILE_LOCATION}${EXCEL_FILE_NAME}
# Get the number of rows
${iTotalRows} = Get Row Count ${PageSheetName}
# Start the for loop here
: FOR ${iRowNum} IN RANGE 1 ${iTotalRows}+1
\ ${ADDRESS} = Read Cell Data By Name ${PageSheetName} A${iRowNum}
\ ${CITY} = Read Cell Data By Name ${PageSheetName} B${iRowNum}
\ ${STATE} = Read Cell Data By Name ${PageSheetName} C${iRowNum}
\ ${ZIP} = Read Cell Data By Name ${PageSheetName} D${iRowNum}
\ ${FINAL_STRING} = Set Variable ${ADDRESS} ${CITY} ${STATE} ${ZIP}
\ ${long} ${lat} nasa-site.Enter the Final String into Site ${FINAL_STRING}
\ Put Number To Cell ${PageSheetName} 4 ${iRowNum} ${long}
\ Put Number To Cell ${PageSheetName} 5 ${iRowNum} ${lat}
Save Excel ${EXCEL_FILE_LOCATION}${EXCEL_FILE_NAME}发布于 2017-12-20 21:26:24
假设问题是关于使用关键字:Put Number To Cell写入文件,我已经删除了关键字的读取部分。变量被替换为其相应的固定值,因为这进一步简化了示例。尽管您的代码中没有指定,但我假定您使用的是可以使用pip install robotframework-excellibrary安装的ExcelLibrary。
下面的示例读取一个excel文件,然后用数字3覆盖第四列中的值。然后将文件保存为新名称,因为我无法覆盖打开的文件。
因为关键字需要数字,所以我使用${4}和${3}来确保将它们转换为数字而不是字符串。不这样做会抛出错误。
*** Settings ***
Library ExcelLibrary
*** Test Cases ***
Open and Read the Excel Record
Open Excel ${EXECDIR}\\Book1.xls
${iTotalRows} = Get Row Count Sheet1
:FOR ${iRowNum} IN RANGE 1 ${iTotalRows}
\ Put Number To Cell Sheet1 ${4} ${iRowNum} ${3}
Save Excel Book2.xls发布于 2020-01-23 22:38:33
久经考验的解决方案,python -2.7.x源- https://groups.google.com/forum/#!topic/robotframework-users/0gxwAa6f8a4
更新文件- site-packages\ExcelLibrary\ExcelLibrary.py中存在的以下两个方法
定义put_string_to_cell(self,sheetname,column,row,value,check_format =):“使用工作表名称将所指示单元格的值设置为参数中给定的字符串。
Arguments:
| Sheet Name (string) | The selected sheet that the cell will be modified from. |
| Column (int) | The column integer value that will be used to modify the cell. |
| Row (int) | The row integer value that will be used to modify the cell. |
| Value (string) | The string value that will be added to the specified sheetname at the specified column and row. |
Example:
| *Keywords* | *Parameters* |
| Open Excel | C:\\Python27\\ExcelRobotTest\\ExcelRobotTest.xls | | | |
| Put String To Cell | TestSheet1 | 0 | 0 | Hello |
"""
if self.wb:
my_sheet_index = self.sheetNames.index(sheetname)
if self.wb.get_sheet(my_sheet_index).ncols > column and self.wb.get_sheet(my_sheet_index).nrows > row and check_format:
cell = self.wb.get_sheet(my_sheet_index).cell(int(row), int(column))
if cell.ctype is XL_CELL_NUMBER:
self.wb.sheets()
if not self.tb:
self.tb = copy(self.wb)
else:
self.wb.sheets()
if not self.tb:
self.tb = copy(self.wb)
if self.tb:
plain = easyxf('')
self.tb.get_sheet(my_sheet_index).write(int(row), int(column), value, plain)def save_excel(self,filename,useTempDir=False):“保存由文件名指示的Excel文件,如果用户需要将文件保存在临时目录中,则可以将useTempDir设置为true。如果布尔值useTempDir设置为true,则取决于运行测试的计算机的操作系统,如果操作系统为Windows,则文件将保存在Temp目录中,如果操作系统不是,则文件将保存在Temp目录中。
Arguments:
| File Name (string) | The name of the of the file to be saved. |
| Use Temporary Directory (default=False) | The file will not be saved in a temporary directory by default. To activate and save the file in a temporary directory, pass 'True' in the variable. |
Example:
| *Keywords* | *Parameters* |
| Open Excel | C:\\Python27\\ExcelRobotTest\\ExcelRobotTest.xls |
| Save Excel | NewExcelRobotTest.xls |
"""
if useTempDir is True:
print '*DEBUG* Got fname %s' % filename
self.tb.save(os.path.join("/", self.tmpDir, filename))
else:
if self.wb:
self.wb.release_resources()
self.tb.save(filename)https://stackoverflow.com/questions/47887123
复制相似问题