def func(items):
"""
items -> [(titleList, dataList).....]
:param items: 包含多个表的数组 (titleList, dataList) -> 表标题列表,表数据列表
:return:
"""
new_items = []
for table in items:
titleHtmlTr = """<td height="19" class="xl65" width="171" style="height: 14.25pt; width: 128pt; padding-top: 1px; padding-right: 1px; padding-left: 1px; font-size: 11pt; font-weight: 700; font-family: 等线; vertical-align: middle; border: 0.5pt solid windowtext; white-space: nowrap; background: rgb(198, 224, 180);">{}</td>"""
dataHtmlTr = """<td height="19" class="xl67" style="height: 14.25pt; border: 0.5pt solid windowtext; padding-top: 1px; padding-right: 1px; padding-left: 1px; font-size: 11pt; font-family: 等线; vertical-align: middle; white-space: nowrap;">{}</td>"""
tableName, titleList, dataList = table
# 将表格数据加入到表格html->td中
DataList = []
for dataLi in dataList:
if len(dataLi) > len(titleList):
raise Exception("item中数据个数不能大于标题个数")
itemList = []
for item in dataLi:
itemList.append(dataHtmlTr.format(item))
itemList = "\n".join(itemList)
trHtml =f""" <tr height="19" style="height:14.25pt">{itemList}</tr>"""
DataList.append(trHtml)
# 将表格标题加入到表格html->td中
titleHtmlTrList = []
for titleLi in titleList:
titleHtmlTrList.append(titleHtmlTr.format(titleLi))
newTitleHtmlTr = "\n".join(titleHtmlTrList)
newDataHtmlTr = "\n".join(DataList)
# iframe 组成单个表格html -> iframe
iframe = """<div>
<span microsoft="" yahei="" ui";="" font-size:="" 14px;="" color:="" rgb(0,="" 0,="" 0);="" background-color:=""
rgba(0,="" font-weight:="" normal;="" font-style:="" normal;text-decoration:="" none;"="">
<div><span microsoft="" yahei="" ui";="" font-size:="" 14px;="" color:="" rgb(0,="" 0,="" 0);=""
background-color:="" rgba(0,="" font-weight:="" normal;="" font-style:="" normal;text-decoration:=""
none;"="">"""+tableName+"""</span></div>
<div>
<table border="0" cellpadding="0" cellspacing="0" width="924" style="border-collapse:collapse;width:693pt">
<!--StartFragment-->
<colgroup>
<col width="171" style="mso-width-source:userset;mso-width-alt:5472;width:128pt">
<col width="171" style="mso-width-source:userset;mso-width-alt:5472;width:128pt">
<col width="494" style="mso-width-source:userset;mso-width-alt:15808;width:371pt">
<col width="88" style="mso-width-source:userset;mso-width-alt:2816;width:66pt">
</colgroup>
<tbody>
"""+newTitleHtmlTr+"""
"""+newDataHtmlTr+""" <!--EndFragment-->
</tbody>
</table>
</div>
<!-- 添加换行间隔-->
<div><br></div>
</div>"""
new_items.append(iframe)
return """<style class="fox_global_style">
div.fox_html_content { line-height: 1.5;}
/* 一些默认样式 */
blockquote { margin-Top: 0px; margin-Bottom: 0px; margin-Left: 0.5em }
ol, ul { margin-Top: 0px; margin-Bottom: 0px; list-style-position: inside; }
p { margin-Top: 0px; margin-Bottom: 0px }
</style>""" + "\n".join(new_items)
if __name__ == '__main__':
items = [
("表格1", ["表格1->标题1","表格1->标题2","表格1->标题3"],[[1,2,3]]),
("表格2", ["表格2->标题1","表格2->标题2","表格2->标题3"],[["a","b","c"]]),
("表格3", ["表格3->标题1","表格3->标题2","表格3->标题3"],[["1a","2b","3c"]]),
("表格生成成功,但是样式统一没变", ["表格3->标题1","表格3->标题2","表格3->标题3","表格4->标题4"],[["1a","2b","3c","4"],["1","2","3","4"],["1","2","3","4"],["1","2","3","4"],["1","2","3","4"],["1","2","3","4"],["1","2","3","4"],["1","2","3","4"]]),
# 单个表数据加入,有几个就生成几个这样的元祖
(
"表格名称",
[
"表格标题1", #标题第1个
"表格标题2...." #标题第2个
],
[ #数据
[1,2], #数据第一行
[1,2] #数据第二行
]
)
]
newHtml = func(items)
print(newHtml)
python 邮件html格式展示
最新推荐文章于 2024-07-25 11:52:08 发布