本小节内容:如何通过python脚本实现添加背景颜色

        #添加Excel用例标题颜色
        workbook = open_workbook(filepath) # 打开Excel工作簿
        new_workbook = copy(workbook) # 复制Excel工作簿
        new_sheet = new_workbook.get_sheet(index)
        pattern = xlwt.Pattern() # Create the Pattern
        pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
        pattern.pattern_fore_colour = 22
        # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
        style = xlwt.XFStyle() # Create the Pattern
        style.pattern = pattern # Add Pattern to Style
        headline_list = ["用例编号","所属模块","子模块","用例名称","请求域名/IP","请求地址","请求方式","请求头部","请求参数","状态码","期望返回结果","实际返回结果","测试结果"]
        for k in range(len(headline_list)):
        new_sheet.write(0,k,headline_list[k], style)

        workbook = open_workbook(filepath)
        sheet1_content1 = workbook.sheet_by_index(index)
        # 读取总的Excel表格的数量,来确定循环次数
        rows = sheet1_content1.nrows
        #测试结果执行成功,显示绿色
        pattern1 = xlwt.Pattern()  # Create the Pattern
        pattern1.pattern = xlwt.Pattern.SOLID_PATTERN  # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
        # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
        style1 = xlwt.XFStyle()  # Create the Pattern
        style1.pattern = pattern1  # Add Pattern to Style

        #测试结果执行失败,显示红色
        pattern2 = xlwt.Pattern()  # Create the Pattern
        pattern2.pattern = xlwt.Pattern.SOLID_PATTERN  # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
        # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
        style2 = xlwt.XFStyle()  # Create the Pattern
        style2.pattern = pattern2  # Add Pattern to Style
        print(rows)
        for i in range(1,rows):
            print(sheet1_content1.cell(i,12).value)
            if sheet1_content1.cell(i,12).value=="测试通过":
                pattern1.pattern_fore_colour = 17
                style1.pattern1=pattern1
                new_sheet.write(i,12,"测试通过",style1)
            else:
                pattern2.pattern_fore_colour = 2
                style2.pattern1=pattern2
                new_sheet.write(i,12,"测试失败",style2)
        new_workbook.save(filepath)

posted @ 2021-01-27 11:11  Camillezxl  阅读(439)  评论(0编辑  收藏  举报