Python-HTML-使用CSS样式发送HTML表格 [英] Python - HTML - Send HTML Table with CSS Style
本文介绍了Python-HTML-使用CSS样式发送HTML表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将带有表格的电子邮件发送为带有某些CSS配置的正文.为此,我有以下代码:
I'm trying to send a email with a table as a body with some CSS configurations. For that I have the following code:
import csv
from tabulate import tabulate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
text = """Hello, Friend.Here is your data:{table}Regards,Me"""
html = """"\
<html>
<head>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
</head>
<body>
<table class="tg">
<tr>
<th class="tg-0lax">Environment</th>
<th class="tg-0lax">Date</th>
<th class="tg-0lax">Error_Type</th>
<th class="tg-0lax">Error_Object</th>
<th class="tg-0lax">Description</th>
</tr>
<tr>
<td class="tg-0lax">DEV</td>
<td class="tg-0lax">15/03/2019</td>
<td class="tg-0lax">ERROR</td>
<td class="tg-0lax">Table</td>
<td class="tg-0lax">More columns than expected</td>
</tr>
</table>
</body>
</html>"""
with open('file.csv') as input_file:
reader = csv.reader(input_file)
data = list(reader)
但是我在添加CSS样式时遇到了一个问题:
However I'm facing with a problem when I add the CSS style:
html = html.format(table=tabulate(data, headers="firstrow", tablefmt="html"))
KeyError: 'border-collapse'
如何在HTML表格中添加该CSS样式?
How can I add that CSS Style on my HTML Table?
非常感谢!
推荐答案
您需要在文本中使用双花括号以避免字符串格式插值.
You need to use double curly-braces in oder to avoid string format interpolation.
例如:
<style type="text/css">
.tg {{border-collapse:collapse;border-spacing:0;}}
.tg td{{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}}
.tg th{{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}}
.tg .tg-0lax{{text-align:left;vertical-align:top}}
</style>
这篇关于Python-HTML-使用CSS样式发送HTML表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文