Skip to content

Instantly share code, notes, and snippets.

@tsundara
Last active July 23, 2020 07:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsundara/8d7d65a9f4dd87ee1ef66d1231dd5c0c to your computer and use it in GitHub Desktop.
Save tsundara/8d7d65a9f4dd87ee1ef66d1231dd5c0c to your computer and use it in GitHub Desktop.
Convert csv file content to html table and then to smtp email
#Purpose : Takes a csv and sends an email in html table format
#usage : python pycsvtotabletomail.py mytable.csv
import pandas as pd
import sys
csvfile=sys.argv[1]
df=pd.read_csv(csvfile)
htmltable=df.to_html(index=False)
htmltable=htmltable.replace('border="1"','border="1" style="border-collapse:collapse"')
emailheader='''From: donotreply@yyourcompanyemail.com
To: abc@yyourcompanyemail.com
Subject: Validation errors
Mime-Version: 1.0
Content-Type: text/html
'''
htmlheader='''<html>
<head>
<style>
table.dataframe {
border: 1px solid #1C6EA4;
background-color: #EEEEEE;
text-align: left;
border-collapse: collapse;
}
table.dataframe td, table.dataframe th {
border: 1px solid #AAAAAA;
padding: 3px 2px;
}
table.dataframe tbody td {
font-size: 13px;
}
table.dataframe tr:nth-child(even) {
background: #D0E4F5;
}
table.dataframe thead {
background: #1C6EA4;
background: -moz-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
background: -webkit-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
background: linear-gradient(to bottom, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
border-bottom: 2px solid #444444;
}
table.dataframe thead th {
font-size: 15px;
font-weight: bold;
color: #FFFFFF;
border-left: 2px solid #D0E4F5;
}
table.dataframe thead th:first-child {
border-left: none;
}
table.dataframe tfoot {
font-size: 14px;
font-weight: bold;
color: #FFFFFF;
background: #D0E4F5;
background: -moz-linear-gradient(top, #dcebf7 0%, #d4e6f6 66%, #D0E4F5 100%);
background: -webkit-linear-gradient(top, #dcebf7 0%, #d4e6f6 66%, #D0E4F5 100%);
background: linear-gradient(to bottom, #dcebf7 0%, #d4e6f6 66%, #D0E4F5 100%);
border-top: 2px solid #444444;
}
table.dataframe tfoot td {
font-size: 14px;
}
table.dataframe tfoot .links {
text-align: right;
}
table.dataframe tfoot .links a{
display: inline-block;
background: #1C6EA4;
color: #FFFFFF;
padding: 2px 8px;
border-radius: 5px;
}
</style>
'''
emailfinal=emailheader + htmlheader + htmltable
print(emailfinal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment