date error and styling issue in python code while reading an excel file and emailing

Viewed 48

I have a code which reads an excel file and generates an image from the data frame which is added to the body of the email everything works fine except I am facing an issue where i am unable to style the 2nd 3rd and 4th column as center align , also the date format seems to be with the timestamp which i am unable to get rid of. Below is the code

import pandas as pd
import plotly.figure_factory as ff
import plotly.graph_objects as go
# import SMTP

pd.options.display.float_format = '{:.4f}'.format
df = pd.read_excel('C:\\Users\Automation\\Droom Technology Dropbox\\Analytics and 
Planning\\Analytics\\13. Trackers\\decision science automation\\Nikhil\\Daily Marketplace 
Tracker.xlsx',sheet_name='Sheet1')
# pd.options.display.max_columns = 500000

#df['date'] = pd.to_datetime(df['date']).dt.date


# df.columns = range(df.shape[1])
lastCloumnsRange = df.shape[1] - 3

df.drop(df.iloc[:, 1:lastCloumnsRange], inplace=True, axis=1)
print(df)
df.dropna(inplace = True)

fig =  ff.create_table(df)
fig.update_layout(
autosize=True,
width=500,
height=400,
)
for i in range(len(fig.layout.annotations)):
fig.layout.annotations[i].font.size = 6
# fig.update_traces(cells_font=dict(size = 15))

fig.write_image("table_plotly.png", scale=2)


########## Mailing Code ###############

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email import encoders
fromaddr = "Shrikant.ravi@xyz.in"
toaddr = ['nikhil.khandelwal@xyz.in']
# instance of MIMEMultipart
msg = MIMEMultipart()

# storing the senders email address  
msg['From'] = fromaddr

# storing the receivers email address
msg['To'] = ", ".join(toaddr)

# storing the subject
msg['Subject'] = "Daily marketplace tracker"

# string to store the body of the mail
body = "Please find attached the graphs for your reference"


# %%

# In[4]:


text = MIMEText('Hello Everyone,\n \nPlease find below the daily Marketplace tracker for your 
reference. \n  \n  \n')
msg.attach(text)


# In[5]:


text1 = MIMEText('<img src="cid:image1">', 'html')
msg.attach(text1)

image = MIMEImage(open('C:\\Users\\Automation\\Desktop\\table_plotly.png', 'rb').read())

# Define the image's ID as referenced in the HTML body above
image.add_header('Content-ID', '<image1>')
msg.attach(image)

filename = "Daily Marketplace Tracker.xlsx"
attachment = open('C:\\Users\Automation\\Droom Technology Dropbox\\Analytics and 
Planning\\Analytics\\13. Trackers\\decision science automation\\Nikhil\\Daily Marketplace 
Tracker.xlsx', "rb")


# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream')

# To change the payload into encoded form
p.set_payload((attachment).read())

# encode into base64
encoders.encode_base64(p)

p.add_header('Content-Disposition', "attachment; filename= %s" % filename)

# attach the instance 'p' to instance 'msg'
msg.attach(p)



# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)

# start TLS for security
s.starttls()

# Authentication
s.login(fromaddr, "xxxx")

# Converts the Multipart msg into a string
text = msg.as_string()

# sending the mail
s.sendmail(fromaddr, toaddr, text)

# terminating the session
s.quit()


# In[ ]:

excel reference link

email body image

0 Answers
Related