运行python代码时出现NameError

时间:2013-05-22 08:49:02

标签: python xlwt

我在运行下面的代码时遇到了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 / pass / Passed表示绿色,则fail / Fail / failed / Failed分别表示红色。

2 个答案:

答案 0 :(得分:2)

passorfail未定义。另外,您正在尝试错误地设置单元格背景颜色。

以下是包含修正的代码(passorfailraw_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"

希望有所帮助。

答案 1 :(得分:0)

如果/ else行很可怕。完整地写出来,你会看到问题所在。

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

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