Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data frame as table inside email body python?

I have a csv file that i need to send as a dataframe inside email body.

However in the output of my email i am unable to see the columns of the csv file and instead it just makes the first row of the csv file as columns.

** Here is my Code:**

import pandas as pd
import csv
from tabulate import tabulate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

me = '[email protected]'
password = 'password'
server = 'smtp.gmail.com:587'
you = '[email protected]'

text = """
Hello, Friend.

Here is your data:

{table}

Regards,

Me"""

html = """
<html>
<head>
<style> 
  table, th, td {{ border: 1px solid black; border-collapse: collapse; }}
  th, td {{ padding: 5px; }}
</style>
</head>
<body><p>Hello, Friend This data is from a data frame.</p>
<p>Here is your data:</p>
{table}
<p>Regards,</p>
<p>Me</p>
</body></html>
"""

# with open('input.csv') as input_file:
#     reader = csv.reader(input_file)
#     data = list(reader)

data = pd.read_csv("MySampleFile.csv")
text = text.format(table=tabulate(data, headers="firstrow", tablefmt="grid"))
html = html.format(table=tabulate(data, headers="firstrow", tablefmt="html"))

message = MIMEMultipart(
    "alternative", None, [MIMEText(text), MIMEText(html,'html')])

message['Subject'] = "First Attempt"
message['From'] = me
message['To'] = you
server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(me, password)
server.sendmail(me, you, message.as_string())
server.quit()
like image 600
Carl Avatar asked Mar 06 '23 19:03

Carl


1 Answers

It seems all i had to do was to extract the respective columns inside my csv file into a separate list that could be passed into tabulate method's headers parameter.

Here is the complete solution

import pandas as pd
import csv
from tabulate import tabulate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

me = '[email protected]'
password = 'password'
server = 'smtp.gmail.com:587'
you = '[email protected]'

text = """
Hello, Friend.

Here is your data:

{table}

Regards,

Me"""

html = """
<html>
<head>
<style> 
 table, th, td {{ border: 1px solid black; border-collapse: collapse; }}
  th, td {{ padding: 5px; }}
</style>
</head>
<body><p>Hello, Friend This data is from a data frame.</p>
<p>Here is your data:</p>
{table}
<p>Regards,</p>
<p>Me</p>
</body></html>
"""

# with open('input.csv') as input_file:
#     reader = csv.reader(input_file)
#     data = list(reader)

df = pd.read_csv("MySampleFile.csv")
col_list = list(df.columns.values)
data = df
# above line took every col inside csv as list
text = text.format(table=tabulate(data, headers=col_list, tablefmt="grid"))
html = html.format(table=tabulate(data, headers=col_list, tablefmt="html"))

message = MIMEMultipart(
    "alternative", None, [MIMEText(text), MIMEText(html,'html')])

message['Subject'] = "First Attempt"
message['From'] = me
message['To'] = you
server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(me, password)
server.sendmail(me, you, message.as_string())
server.quit()
like image 141
Carl Avatar answered Mar 15 '23 15:03

Carl