如何在HTML脚本中集成HTML代码? [英] How do I integrate a HTML code in a Python Script?

查看:95
本文介绍了如何在HTML脚本中集成HTML代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python代码,该代码为Reddit的数据框架创建报告,并将其转换为简单的HTML,然后通过电子邮件发送出去.下面是代码:

I have a Python code that creates a report for a data frame from Reddit, and converts it to simple HTML and then email's it out. Below is the code:

#Clean all the Dataframes
test_clean = clean(test_test_df)
brand_clean = clean(brands_df)
competitor_clean = clean(competitors_df)
#Convert to HTML
test_html = test_clean.render()
brand_html = brand_clean.render()
competitor_html = competitor_clean.render()


# In[27]:


brand_clean


# # Email Integration

# #### Import Libraries for Email

# In[ ]:


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


# #### Send Email If No Data is Available

# In[ ]:


if test_test_df.empty:
    today = str(date.today())

    fromaddr = "email@email.com"
    toaddr = "email@email.com"
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = "Daily Reddit Monitor " + today


    message = "There are no relevant posts above the 100 score threshold today!"

    #email = df_complete.render()

    part1 = MIMEText(message, 'plain')


    msg.attach(part1)
    #msg.attach(part2)

    server = smtplib.SMTP('smtp.postmarkapp.com', 587)
    server.starttls()
    server.login('API-KEY", "API-KEY')
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()
    IpyExit 

收到的电子邮件格式非常简单.我希望该电子邮件看起来不错,因此使用HTML Tables内联CSS编写了带有标头图像徽标等内容的HTML代码,简而言之是新闻信函的HTML代码.现在,我希望该Python脚本在发送电子邮件时使用我的HTML代码,以便在收件箱中接收到的电子邮件看起来像一封新闻信.有什么建议或解决方案可以实现吗?

The email that is received is very simple in format. I wanted that email to look good so wrote a HTML code with header image logo etc using HTML Tables inline CSS, in short a HTML code for news letter. Now I want that Python script to use my HTML code while sending the email so that the email when received in Inbox looks good like a news letter. Any suggestion or solution how I can achieve this?

下面是我的HTML代码.

Below is my HTML code.

<table width="689" border="0" cellspacing="0" cellpadding="1" align="center" bgcolor="#353A71">
<tr>
     <td valign="middle" align="center">

     <table width="689" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" align="center">
    <tr align="left"> 
         <td valign="top" colspan="2"> 

          <table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
        <tr> 
             <td width="0%">&nbsp;</td>
            <td valign="top" width="100%">

               <center><h1 style="font-family:helvetica;">Top Reddit Posts</h1></center>



                <td width="0%">&nbsp;</td>
        </tr>
        <tr>

             <td width="0%">&nbsp;</td>
               <td>&nbsp;</td>     

            <td width="0%">&nbsp;</td>
        </tr>
          <tr> 
             <td width="0%" bgcolor="#FFFFFF">&nbsp;</td>
            <td align="center" class="profileCaptionWhiteBold" width="100%" valign="top" bgcolor="#FFFFFF"> 
               </td>
               <td width="0%" bgcolor="#FFFFFF">&nbsp;</td>
        </tr>
        </table>

所以我希望脚本的输出继续执行:

So I want the out put of the script to go after:

Reddit上的热门帖子

Top Reddit Posts

推荐答案

您的示例代码不是很清楚,但是我认为您只是想将现有的HTML片段(来自Reddit的数据框报告)嵌入到其中一个更大的HTML页面,很好地展示了它.

Your sample code wasn't very clear, but I think you are just trying to embed an existing HTML fragment (the report for a data frame from Reddit) into a larger HTML page that presents it nicely.

为此,您可以简单地使用多行字符串中包含的模板,然后在其中替换地标{}的值:

To do this, you can simply use a template held in a multi-line string then substitute values for placemarkers {} within it:

# Placeholder for current html report from dataframe (replace with your code)
df = pd.DataFrame([{'Title': 'Story 1 title', 'Description': 'Story 1 description'}])
redditHTML = df.to_html()

# HTML news letter template
template='''
<table width="689" border="0" cellspacing="0" cellpadding="1" align="center" bgcolor="#353A71">
    <tr>
        <td valign="middle" align="center">
            <table width="689" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" align="center">
                <tr align="left"> 
                    <td valign="top" colspan="2"> 
                        <table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
                            <tr> 
                                <td width="0%">&nbsp;</td>
                                <td valign="top" width="100%">
                                    <center><h1 style="font-family:helvetica;">Top Reddit Posts</h1></center>

                                    {}

                                </td>
                                <td width="0%">&nbsp;</td>
                            </tr>
                            <tr>
                                <td width="0%">&nbsp;</td>
                                <td>&nbsp;</td>     
                                <td width="0%">&nbsp;</td>
                            </tr>
                            <tr> 
                                <td width="0%" bgcolor="#FFFFFF">&nbsp;</td>
                                <td align="center" class="profileCaptionWhiteBold" width="100%" valign="top" bgcolor="#FFFFFF"></td>
                                <td width="0%" bgcolor="#FFFFFF">&nbsp;</td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
'''

completeHTML = template.format(redditHTML)

msg.attach(MIMEText(completeHTML, 'html'))

请注意,您的HTML代码示例缺少</td>来关闭包含热门Reddit帖子的部分,并且缺少结尾的</td> </tr> </table>来完成新闻信

Note that your HTML code example was missing a </td> to close the section containing the Top Reddit Posts, plus was missing the trailing </td> </tr> </table> to complete the news letter

这篇关于如何在HTML脚本中集成HTML代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆