3

运行下面的代码时出现 NameError。我该如何解决这个问题?

代码:

import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Status')

passed = xlwt.easyxf('pattern: back_color green')
failed = xlwt.easyxf('pattern: back_color red')


color = (passed if passorfail in ['pass','Passed','passed'] else
    (failed if passorfail in ['fail','Failed','failed'] else xlwt.easyxf()))

worksheet.write_merge(6, 6, 3, 3,passorfail, style = color)

workbook.save('passfail2.xls')
print "Completed"

输出:

Traceback (most recent call last):
  File "G:\airspan_eclipse\Excel_Gen\passfail2.py", line 9, in <module>
    color = (passed if passorfail in ['pass','Passed','passed'] else
NameError: name 'passorfail' is not defined

预期结果:

Excel 单元格颜色将根据文本更改,如果 pass/Pass/passed/Passed 分别表示绿色,fail/Fail/failed/Failed 分别表示红色。

4

2 回答 2

2

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"

希望有帮助。

于 2013-05-22T09:07:39.863 回答
0

if/else 行太可怕了。把它完整地写出来,你会发现问题出在哪里。

if passorfail in ['pass','Passed','passed']:
    color = passed
elif passorfail in ['fail','Failed','failed']:
    color = failed
else:
    color = xlwt.easyxf()

现在很明显你指的是passorfail,但你还没有定义它。

于 2013-05-22T09:06:16.623 回答