How do I integrate a HTML code in a Python Script?

Say the HTML you have generated elsewhere is contained in the string messageHTML, then all you have to add is:

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

Leave the plain text in as well, so the two lines will look like

msg.attach(MIMEText(messagePlain, 'plain'))
msg.attach(MIMEText(messageHTML, 'html'))

To set up the HTML, create a variable messageHTML. Then you could create the table like so (assuming you want 1 row, with 2 columns, data_1 and data_2):

messagePlain = data_1 + " " + data_2
messageHTML = '<table><tr><td>' + data_1 + '</td><td>' + data_2 + '</td></tr></table>'

msg.attach(MIMEText(messagePlain, 'plain'))
msg.attach(MIMEText(messageHTML, 'html'))

I'd recommend starting off with a simple table, maybe not even with dynamically obtained data, to ensure that the HTML Is rendering correctly when you send it, and then it extend the HTML to the content and style you want later.


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'))

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