1. 程式人生 > >Python郵件傳送之HTML表格快速建立

Python郵件傳送之HTML表格快速建立

有時候需要傳送帶表格郵件,可能我們的資料是從資料庫匯出,或者是來自外部的csv或者excel檔案,不管是哪種形式,到了Python,都是DataFrame格式,這時候,我們想將表格嵌入到HTML中,別急!有捷徑!

1、Head通用部分

head = \
        """
        <head>
            <meta charset="utf-8">
            <STYLE TYPE="text/css" MEDIA=screen>

                table.dataframe {
                    border-collapse: collapse;
                    border: 2px solid #a19da2;
                    /*居中顯示整個表格*/
                    margin: auto;
                }

                table.dataframe thead {
                    border: 2px solid #91c6e1;
                    background: #f1f1f1;
                    padding: 10px 10px 10px 10px;
                    color: #333333;
                }

                table.dataframe tbody {
                    border: 2px solid #91c6e1;
                    padding: 10px 10px 10px 10px;
                }

                table.dataframe tr {

                }

                table.dataframe th {
                    vertical-align: top;
                    font-size: 14px;
                    padding: 10px 10px 10px 10px;
                    color: #105de3;
                    font-family: arial;
                    text-align: center;
                }

                table.dataframe td {
                    text-align: center;
                    padding: 10px 10px 10px 10px;
                }

                body {
                    font-family: 宋體;
                }

                h1 {
                    color: #5db446
                }

                div.header h2 {
                    color: #0002e3;
                    font-family: 黑體;
                }

                div.content h2 {
                    text-align: center;
                    font-size: 28px;
                    text-shadow: 2px 2px 1px #de4040;
                    color: #fff;
                    font-weight: bold;
                    background-color: #008eb7;
                    line-height: 1.5;
                    margin: 20px 0;
                    box-shadow: 10px 10px 5px #888888;
                    border-radius: 5px;
                }

                h3 {
                    font-size: 22px;
                    background-color: rgba(0, 2, 227, 0.71);
                    text-shadow: 2px 2px 1px #de4040;
                    color: rgba(239, 241, 234, 0.99);
                    line-height: 1.5;
                }

                h4 {
                    color: #e10092;
                    font-family: 楷體;
                    font-size: 20px;
                    text-align: center;
                }

                td img {
                    /*width: 60px;*/
                    max-width: 300px;
                    max-height: 300px;
                }

            </STYLE>
        </head>
        """

2、Body部分

body = \
        """
        <body>

        <div align="center" class="header">
            <!--標題部分的資訊-->
            <h1 align="center">請輸入你的標題</h1>
            <h2 align="center">{yesterday}</h2>
        </div>

        <hr>

        <div class="content">
            <!--正文內容-->
            <h2> </h2>

            <div>
                <h4></h4>
                {df_html}

            </div>
            <hr>

            <p style="text-align: center">

            </p>
        </div>
        </body>
        """
.format(yesterday=get_yesterday(),df_html=df_html)

3、HTML構建

html_msg = "<html>" + head + body + "</html>"

4、DataFrame資料的HTML表格轉化

html_msg = "<html>" + head + body + "</html>"
html_msg = html_msg.replace('\n','').encode("utf-8")

上面的程式碼具有複用性,關鍵的關鍵就是df_htmlr如何生成。

pd.set_option('display.max_colwidth'
, -1) #設定表格資料完全顯示(不出現省略號) df_html = filter_merge_data.to_html(escape=False) #DataFrame資料轉化為HTML表格形式

拼接後的HTML可能含有換行符\n,需要將其去掉,最終得到的html_msg即為傳送郵件的HTML內容。

html_msg = "<html>" + head + body + "</html>"
html_msg = html_msg.replace('\n','').encode("utf-8")

傳送郵件的部分就不介紹了,藉助email以及smtplib就可以了。

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.header import Header
import smtplib

這裡寫圖片描述