如何用Python根据PASS/FAIL状态为HTML表格单元格设置背景色
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:
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#90EE90for bright green (#008000) or#FFCCCBfor bright red (#FF0000) if you prefer bolder colors. - Added
text-align: centerto make the status text look cleaner in the cell.
- Takes a status string and returns a
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.
- Removed the unnecessary
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.
- Added a
Safer File Handling:
- Used a
withstatement to open the HTML file, which automatically closes it after writing—this avoids potential file corruption issues from forgottenclose()calls.
- Used a
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

