You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python根据PASS/FAIL状态为HTML表格单元格设置背景色

Fixing Colored Status Cells in Your Python HTML Report

I see exactly what you need—let's get those PASS/FAIL cells showing green and red backgrounds respectively. The solution is to add inline CSS styles to the <td> elements that contain your status values, based on whether they're "PASS" or "FAIL".

Here's the modified code with this functionality, plus a few small cleanups to make your report look more polished:

def get_colored_status_cell(status):
    # Use soft, readable colors for status cells
    bg_color = "#90EE90" if status == "PASS" else "#FFCCCB"
    return f'<td style="background-color: {bg_color}; text-align: center;">{status}</td>'

def generateReport(reportpath, dict1, dict2):
    html = """<html>
<head>
<style>
th { background-color: #f0f0f0; text-align: left; }
table { border-collapse: collapse; width: 100%; }
td, th { padding: 8px; border: 1px solid #ddd; }
</style>
</head>
<body>
<table>
<th>FileName</th>
<th>MD5 (Dict1)</th>
<th>MD5 (Dict2)</th>
<th><span style="color: red;">MD5 Result</span></th>
<th>Sha1 (Dict1)</th>
<th>Sha1 (Dict2)</th>
<th><span style="color: red;">Sha1 Result</span></th>
<th>Sha256 (Dict1)</th>
<th>Sha256 (Dict2)</th>
<th><span style="color: red;">Sha256 Result</span></th>"""
    
    for key in dict1:
        # Removed unnecessary str() wrapping on equality checks
        status1 = 'PASS' if dict1[key][0] == dict2[key][0] else 'FAIL'
        status2 = 'PASS' if dict1[key][1] == dict2[key][1] else 'FAIL'
        status3 = 'PASS' if dict1[key][2] == dict2[key][2] else 'FAIL'
        
        # Use our helper function to create colored status cells
        status_cell1 = get_colored_status_cell(status1)
        status_cell2 = get_colored_status_cell(status2)
        status_cell3 = get_colored_status_cell(status3)
        
        html += f"""<tr>
<td>{key}</td>
<td>{dict1[key][0]}</td>
<td>{dict2[key][0]}</td>
{status_cell1}
<td>{dict1[key][1]}</td>
<td>{dict2[key][1]}</td>
{status_cell2}
<td>{dict1[key][2]}</td>
<td>{dict2[key][2]}</td>
{status_cell3}
</tr>"""
    
    html += "</table></body></html>"
    
    # Use with statement for safer, automatic file handling
    with open(reportpath, "w") as newhtml:
        newhtml.write(html)

Key Changes Explained:

  1. Helper Function get_colored_status_cell:

    • Takes a status string and returns a <td> element with the correct background color. I used soft, eye-friendly shades, but you can swap #90EE90 for bright green (#008000) or #FFCCCB for bright red (#FF0000) if you prefer bolder colors.
    • Added text-align: center to make the status text look cleaner in the cell.
  2. Cleaned Up Comparisons:

    • Removed the unnecessary str() wrapping around your equality checks—comparing hash values directly works perfectly without converting the boolean result to a string.
  3. Polished HTML Styling:

    • Added a <style> section to make the table look more professional: collapsed borders, cell padding, and a light gray header background.
    • Renamed header labels to clarify which hash comes from which dictionary (e.g., "MD5 (Dict1)" instead of duplicate "MD5" headers).
    • Replaced the outdated <font> tag with a modern <span> using inline CSS for result headers.
  4. Safer File Handling:

    • Used a with statement to open the HTML file, which automatically closes it after writing—this avoids potential file corruption issues from forgotten close() calls.

Now when you generate your report, every "PASS" status will have a light green background, and every "FAIL" will have a light red background—making validation results instantly visible at a glance.

内容的提问来源于stack exchange,提问作者user3232223

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:13:26