import xlrd
import xlwt
import pandas as pd
# 打开modify.xls和bench.xls文件
modify_book = xlrd.open_workbook('modify.xls')
bench_book = xlrd.open_workbook('bench.xls')
# 获取modify.xls文件中的工作表
modify_sheet = modify_book.sheet_by_index(0)
# 获取bench.xls文件中的工作表
bench_sheet = bench_book.sheet_by_index(0)
# 创建一个空的DataFrame
results_df = pd.DataFrame()
# 遍历modify.xls文件中的每一行
for row in range(modify_sheet.nrows):
# 遍历每一列
for col in range(modify_sheet.ncols):
# 判断当前行和列是否存在于bench.xls文件中
if row < bench_sheet.nrows and col < bench_sheet.ncols:
# 获取modify.xls和bench.xls中的单元格的值
modify_value = modify_sheet.cell_value(row, col)
bench_value = bench_sheet.cell_value(row, col)
# 如果modify.xls中的值大于bench.xls中的值,则标记为红色
if modify_value > bench_value:
style = 'color: white; background-color: red;'
# 如果modify.xls中的值小于bench.xls中的值,则标记为绿色
elif modify_value < bench_value:
style = 'color: white; background-color: green;'
# 否则不标记颜色
else:
style = ''
else:
# 如果当前行和列不存在于bench.xls文件中,则直接将modify.xls中的值写入DataFrame中
modify_value = modify_sheet.cell_value(row, col)
bench_value = ''
style = ''
# 将标记了颜色的单元格写入到DataFrame中
results_df.at[row, col] = f'<td class="result" style="{style}">{modify_value}</td>'
# 将DataFrame转换为HTML表格
results_html = results_df.to_html(header=False, index=False, escape=False)
# 生成外部样式表
css = """
.result {
padding: 5px;
border: 1px solid black;
}
"""
# 将样式表写入到文件中
with open('results.css', 'w') as file:
file.write(css)
# 在HTML文档中引用样式表
results_html = f"""
<html>
<head>
<link rel="stylesheet" href="results.css">
</head>
<body>
{results_html}
</body>
</html>
"""
# 将HTML表格写入到文件中
with open('results.html', 'w') as file:
file.write(results_html)
使用以上脚本生成了results.html,手动打开,里面的数据都是有颜色的,但是在CI/CD中使用“HTML Publisher Plugin”网页插件的方法去显示,或者发送邮件附件的方法去显示,网页都没有颜色,修改以上脚本,在生成html的同时,生成配套的CSS样式表,并且让样式表正确链接,最终在CI/CD中使用HTML Publisher Plugin或者发送邮件的时候html中的数据都可以显示颜色。
从网页看颜色显示有报错: 0Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='), or a nonce ('nonce-...') is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present.
5Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-uq3F7ELN5OOV9/Nw5lGzDXp7EO378o2BHRBqu9wvqG4='), or a nonce ('nonce-...') is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present.
results.html:24 Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-X7jUdIzxmddpAatxF6OBCFXgSt7uY9rgSWOuVH1ke48='), or a nonce ('nonce-...') is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present.
具体在以上脚本中修改来解决问题,然后仍然保留原来的功能,html中的数据有颜色
要解决“内容安全策略”指令的问题,可以按如下方式修改脚本:
1.将“unsafe-inline”关键字添加到网站内容安全策略中的“style-src”指令中。这将允许内联样式应用于HTML。
2.不要在HTML表中使用内联样式,而是在单独的CSS文件中定义样式,并在HTML文件中链接到该文件。这将确保在不违反内容安全策略的情况下应用样式。
以下是包含这些更改的脚本的更新版本:
import xlrd
import xlwt
import pandas as pd
# 打开modify.xls和bench.xls文件
modify_book = xlrd.open_workbook('modify.xls')
bench_book = xlrd.open_workbook('bench.xls')
# 获取modify.xls文件中的工作表
modify_sheet = modify_book.sheet_by_index(0)
# 获取bench.xls文件中的工作表
bench_sheet = bench_book.sheet_by_index(0)
# 创建一个空的DataFrame
results_df = pd.DataFrame()
# 遍历modify.xls文件中的每一行
for row in range(modify_sheet.nrows):
# 遍历每一列
for col in range(modify_sheet.ncols):
# 判断当前行和列是否存在于bench.xls文件中
if row < bench_sheet.nrows and col < bench_sheet.ncols:
# 获取modify.xls和bench.xls中的单元格的值
modify_value = modify_sheet.cell_value(row, col)
bench_value = bench_sheet.cell_value(row, col)
# 如果modify.xls中的值大于bench.xls中的值,则标记为红色
if modify_value > bench_value:
style = 'bg-red'
# 如果modify.xls中的值小于bench.xls中的值,则标记为绿色
elif modify_value < bench_value:
style = 'bg-green'
# 否则不标记颜色
else:
style = ''
else:
# 如果当前行和列不存在于bench.xls文件中,则直接将modify.xls中的值写入DataFrame中
modify_value = modify_sheet.cell_value(row, col)
bench_value = ''
style = ''
# 将标记了颜色的单元格写入到DataFrame中
results_df.at[row, col] = f'<td class="result {style}">{modify_value}</td>'
# 将DataFrame转换为HTML表格
results_html = results_df.to_html(header=False, index=False, escape=False)
# 生成外部样式表
css = """
.result {
padding: 5px;
border: 1px solid black;
}
.bg-red {
color: white;
background-color: red;
}
.bg-green {
color: white;
background-color: green;
}
"""
# 将样式表写入到文件中
with open('results.css', 'w') as file:
file.write(css)
# 在HTML文档中引用样式表
results_html = f"""
<html>
<head>
<link rel="stylesheet" href="results.css">
<meta http-equiv="Content-Security-Policy" content="style-src 'self' 'unsafe-inline'">
</head>
<body>
{results_html}
</body>
</html>
"""
# 将HTML表格写入到文件中
with open('results.html', 'w') as file:
file.write(results_html)
在这个更新的脚本中,我们定义了两个CSS类“bg-red”和“bg-green”,分别应用红色和绿色背景色。我们还在HTML文件的内容安全策略中的“style-src”指令中添加了“unsafe-inline”关键字。最后,我们链接到HTML文件中的外部CSS文件。