Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据条件为HTML表体文本赋予颜色_Html_Python 3.x - Fatal编程技术网

根据条件为HTML表体文本赋予颜色

根据条件为HTML表体文本赋予颜色,html,python-3.x,Html,Python 3.x,我正在使用python发送电子邮件正文中的html表。html表格由作业状态组成,我需要以红色和粗体突出显示失败的作业。 我尝试过不同的方法,包括将失败和成功的工作分开,制作单独的html表格,并在最后用棍棒打它们。但即使在用棍棒打第二张表格之后,第二张表格也有一个额外的边界 PFB我用来发送html表的代码 import pandas df = pandas.read_excel("C:\\"+os.environ["HOMEPATH"]+"\\Desktop\\Daily Monitori

我正在使用python发送电子邮件正文中的html表。html表格由作业状态组成,我需要以红色和粗体突出显示失败的作业。 我尝试过不同的方法,包括将失败和成功的工作分开,制作单独的html表格,并在最后用棍棒打它们。但即使在用棍棒打第二张表格之后,第二张表格也有一个额外的边界

PFB我用来发送html表的代码

import pandas
df = pandas.read_excel("C:\\"+os.environ["HOMEPATH"]+"\\Desktop\\Daily 
Monitoring.xlsx", sheetname='Status Sheet')

import tabulate
html = """
<html>
<head>
<style> 
 table, th, td {{ border: 1px solid black; border-collapse: collapse; }}
  th, td {{ padding: 5px; }}
</style>
</head>
<body><p>Hi All,</p>

<p> Kindly find below the monitoring result:  </p>
{table}

</body></html>
"""


col_list=list(df.columns.values)
html = html.format(table=tabulate.tabulate(df, headers=col_list, tablefmt="html",showindex=False))
导入熊猫
df=pandas.read\u excel(“C:\\”+os.environ[“HOMEPATH”]+“\\Desktop\\Daily”
Monitoring.xlsx“,sheetname='Status Sheet')
进口表格
html=”“”
表,th,td{{边框:1px纯黑色;边框折叠:折叠;}
th,td{{padding:5px;}}
大家好

请查看以下监测结果:

{table} """ col_list=list(df.columns.values) html=html.format(table=tablate.tablate(df,headers=col_list,tablefmt=“html”,showindex=False))

在css中,有一个:n子属性可以用来实现它。我想

`table:nth-child(2) > td 
{ 
color:red;
font-weight:bold;
}
`
因为您知道哪些元素受到影响,所以必须重复多次

一个更干净的解决方案是将每个表单元格的内容放入一个跨度中,并给它一个类。 然后编写这些类,就完成了。 像


A.
B

自己把pandas表改写成字符串怎么样

table = ['<table>']

for row in range (5): #depends on your df size

   table.append('<tr>')

   for col in range (5): #depends on your df size

      if df.iloc[row, col] == 'Failed'

         table.append('<td class=\"failed\">Failed</td>')
      else
         table.append('<td>' + df.iloc[row, col] + '</td>')

   table.append('</tr>')

table.append('</table>')
table = '\n'.join(table)
table=[']
对于范围(5)内的行:#取决于您的df大小
表.追加(“”)
对于范围(5)中的col:#取决于您的df大小
如果df.iloc[行,列]=“失败”
table.append('失败')
其他的
table.append(“”+df.iloc[行,列]+“”)
表.追加(“”)
表.追加(“”)
表='\n'.联接(表)

还没有运行过该代码,但希望您能有这样的想法O:-)

谢谢@Martin,对您的代码做了一些更改,它成功了!
table = ['<table>']

for row in range (5): #depends on your df size

   table.append('<tr>')

   for col in range (5): #depends on your df size

      if df.iloc[row, col] == 'Failed'

         table.append('<td class=\"failed\">Failed</td>')
      else
         table.append('<td>' + df.iloc[row, col] + '</td>')

   table.append('</tr>')

table.append('</table>')
table = '\n'.join(table)