python 邮件html格式展示




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)



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_JackSparrow

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值