簡體   English   中英

使用電子郵件中的HTML並發發送兩個熊貓數據幀

[英]Sending two Pandas dataframes side-by-side using HTML in email

我正嘗試通過電子郵件發送有關我的股票投資組合創建的一些摘要。 我正在使用Python + Pandas進行計算,並使用email.mime模塊通過電子郵件發送html。

我正在使用Pandas的to_html方法和email.mime模塊將html包含在電子郵件中:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

import numpy as np
import pandas as pd
import pickle
from utils import mail


def send_fancy_mail(subject, text_message, html_message):

    msg = MIMEMultipart('alternative')
    msg['Subject'] = subject
    msg['From'] = 'mymail@domain.com'
    msg['To'] = settings.MAIL_RECIPIENTS

    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(text_message, 'plain')
    part2 = MIMEText(html_message, 'html')
    msg.attach(part1)
    msg.attach(part2)

    # Send the message via our own SMTP server.
    s = smtplib.SMTP_SSL(settings.SMTP_HOST)
    s.login(settings.SMTP_USER, settings.SMTP_PASSWORD)
    s.send_message(msg)
    s.quit()

fake_or_load = 'fake'

if fake_or_load == 'fake':
    n_signals = 20
    symbols = np.round(1000*np.random.rand(n_signals)).astype(int)
    direction = ["Buy" for i in range(n_signals//2)]
    direction.extend(["Sell" for i in range(n_signals//2)])
    quantity = np.round(10000*np.random.rand(n_signals)).astype(int)

    portfolio = pd.DataFrame({'symbols': symbols, 'direction': direction, 'quantity': quantity})

elif fake_or_load == 'load':
    with open('c:\\\\temp\\signals_list', 'rb') as fp:
        signals = pickle.load(fp)

    portfolio = pd.DataFrame(signals)
    portfolio.rename(index=str, inplace=True, columns={0: "symbol", 1: "direction", 2: "quantity"})


shares_to_buy = portfolio[portfolio['direction'] == 'Buy'].copy()
shares_to_buy.sort_values(by='quantity', inplace=True, ascending=False)

shares_to_sell = portfolio[portfolio['direction'] == 'Sell'].copy()
shares_to_sell.sort_values(by='quantity', inplace=True, ascending=False)

# The basic way to convert portfolio to html:
html_to_buy = shares_to_buy.to_html(index=False, header=True, col_space=20, justify='center')
html_to_sell = shares_to_sell.to_html(index=False, header=True, col_space=20, justify='center')

mail_body = "Test Message"
css = """
.div {
    flex-direction: row;
}
"""

html_body = """
<html>
  <head>
  <style>{}</style>
  </head>
  <body>
  <div>{}</div>
  <div>{}</div>
  </body>
</html>
""".format(css, html_to_buy, html_to_sell)

mail.send_fancy_mail("Test Mail", mail_body, html_body)

我想在電子郵件正文中並排放置兩列,最好也能調節列寬。 是否可以使表響應?

如果要創建兩列,請用下表替換兩個div。 Div的支持與所有電子郵件客戶端上的表不同。

  <table style="width:100%"> <tr> <td>column 1</td> <td>column 2</td> </tr> </table> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM