python xlutils的简单使用记录

python 接口自动化中将返回的结果写到另一个新的excel文件中如何处理?
1.安装
推荐xlutils模块
pip install xluitilspython xlutils的简单使用记录_第1张图片
2.使用方法

def optration():
    """
    接口测试:将得到的结果写到一个新的excel文件中。
    copy()复制是从内存中复制,并不是一个文件路径,并且获取sheet时必须使用下标不可以使用sheet名字
    :return:
    """
    from xlutils.copy import copy
    import xlrd
    # 指定formatting_info=True,默认formatting_info=Flase且当源文件和保存文件格式不一样时容易报错!
    wb = xlrd.open_workbook(
        filename=r"C:\Users\Administrator\Desktop\用例模板.xls",
        formatting_info=True)
    new_wbook = copy(wb)
    new_sheet = new_wbook.get_sheet(2)
    new_sheet.write(
        19, 6, "调用xlrd.open_workbook()时,如果不指定formatting_info=True,那么修改后整个文档"
        "的样式会丢失。对一个单元格进行write操作时,如果不指定样式,也会将原来的样式丢失。")
    new_wbook.save("test.xls")


if __name__ == '__main__':
    optration()

3、结果,查看新生成的test.xls文件。

在这里插入图片描述
个人工作记录!!!

你可能感兴趣的:(python)