Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

4 of 4
added 1443 characters in body
Piyush Patil
  • 14.6k
  • 8
  • 45
  • 59

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

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 protected]"
 toaddr = "[email protected]"
 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 

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?

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:

Top Reddit Posts
Piyush Patil
  • 14.6k
  • 8
  • 45
  • 59
default

AltStyle によって変換されたページ (->オリジナル) /