passorfail
没有定义。另外,您正在尝试错误地设置单元格背景颜色。
这是带有修复的代码(passorfail
从中读取raw_input()
):
import xlwt
with open('input.txt', 'r') as f:
passorfail = f.read()
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Status')
style = xlwt.easyxf()
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
if passorfail in ['pass', 'Passed', 'passed']:
pattern.pattern_fore_colour = xlwt.Style.colour_map['green']
elif passorfail in ['fail', 'Failed', 'failed']:
pattern.pattern_fore_colour = xlwt.Style.colour_map['red']
style.pattern = pattern
worksheet.write_merge(6, 6, 3, 3, passorfail, style=style)
workbook.save('passfail2.xls')
print "Completed"
编辑:
这是从测试结果字典生成报告的代码:
import xlwt
def generate_styles(colours):
styles = {}
for colour in colours:
style = xlwt.easyxf()
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = xlwt.Style.colour_map[colour]
style.pattern = pattern
styles[colour] = style
return styles
def generate_report(results, name):
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Status')
styles = generate_styles(['green', 'red'])
# write headers
worksheet.write(0, 0, 'Test Name')
worksheet.write(0, 1, 'Test Result')
# write test results
row = 1
for test_name, test_result in results.iteritems():
if test_result in ['pass', 'Passed', 'passed']:
style = styles['green']
elif test_result in ['fail', 'Failed', 'failed']:
style = styles['red']
else:
style = None
worksheet.write(row, 0, test_name, style=style)
worksheet.write(row, 1, test_result, style=style)
row += 1
workbook.save(name)
# assuming test results is a dictionary
results = {'TC01': 'passed', 'TC02': 'failed', 'TC03': 'pass'}
generate_report(results, 'report.xls')
print "Completed"
希望有帮助。