excel(xls)写操作期间出错

时间:2018-01-17 02:11:43

标签: excel robotframework

我从以下链接安装了excel库: ExcelLibrary

我尝试使用以下代码在Excel中写入数据:

1)Indexerror: list index out of range
2)AttributeError: 'Nonetype'object has no attribute 'save'

当我执行此脚本时,它会显示以下错误:

rand()

有关解决此错误的任何解决方法/建议都会有所帮助

1 个答案:

答案 0 :(得分:1)

这个ExcelLibrary容易出错,有一个帖子围绕它进行了一些有价值的讨论。

https://groups.google.com/forum/#!msg/robotframework-users/GpAWczezNHk/kHWCWKA7YQcJ

我将在此列出格式化代码的步骤

1)您需要编辑D:\ Python27 \ Lib \ site-packages \ ExcelLibrary \ ExcelLibrary.py文件

 def put_string_to_cell(self, sheetname, column, row, value):
            """
            Using the sheet name the value of the indicated cell is set to be the string given in the parameter.

            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 |

            """
            def put_string_to_cell(self, sheetname, column, row, value):

                if self.wb:
                    my_sheet_index = self.sheetNames.index(sheetname)
                if not self.tb:
                    self.wb.sheets()
                    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 put_date_to_cell(self, sheetname, column, row, value):
        """
        Using the sheet name the value of the indicated cell is set to be the date given in the parameter.

        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 (int)                       | The integer value containing a date 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 Date To Cell     |  TestSheet1                                        |  0  |  0  |  12.3.1999 |

        """
        if self.wb:
            my_sheet_index = self.sheetNames.index(sheetname)
            # cell = self.wb.get_sheet(my_sheet_index).cell(int(row), int(column))
            # if cell.ctype is XL_CELL_DATE:
            if not self.tb:
                self.wb.sheets()
                self.tb = copy(self.wb)
        if self.tb:
            print(value)
            # dt = value.split('.')
            # dti = [int(dt[2]), int(dt[1]), int(dt[0])]
            # print(dt, dti)
            ymd = datetime.strptime(value, '%d-%m-%Y')
            print(ymd)
            plain = easyxf('', num_format_str='d-M-yyyy')
            self.tb.get_sheet(my_sheet_index).write(int(row), int(column), ymd, plain)


def put_number_to_cell(self, sheetname, column, row, value):
    """
    Using the sheet name the value of the indicated cell is set to be the number given in the parameter.

    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 (int)         | The integer 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 Number To Cell   |  TestSheet1                                        |  0  |  0  |  34  |

    """
    if self.wb:
        my_sheet_index = self.sheetNames.index(sheetname)
        # cell = self.wb.get_sheet(my_sheet_index).cell(int(row), int(column))
        # if cell.ctype is XL_CELL_NUMBER:
    if not self.tb:
        self.wb.sheets()
        self.tb = copy(self.wb)
    if self.tb:
        plain = easyxf('')
        self.tb.get_sheet(my_sheet_index).write(int(row), int(column), float(value), plain)

2)之后转到D:\ Python27 \ Lib \ site-packages \ ExcelLibrary

运行

python __init__.py

这会将编译后的文件放入ExcelLibrary包中。

此代码将帮助您解决错误

1)Indexerror:列表索引超出范围

但是,我仍然无法保存文件,而且此代码会出错

IOError: [Errno 22] invalid mode ('w+b') or filename: u'test1.xls'

建议:

最好在python中编写自己的自定义库并在RF中使用它,如果你想知道如何创建自定义库,这里有一个链接

http://learningbysimpleway.blogspot.in/2018/01/robotframework-have-rich-set-of_7.html