1

I am using win32com.client in Python to send an email.

However I want the body of the email to be a table (HTML- formatted table), I can do it in an Excel first and then copy and paste (but how?), or directly edit the corresponding Pandas data frame.

newMail.body = my_table which is a Pandas data frame didn't work.

So I'm wondering if there is smarter ways for example, to combine Excel with Outlook apps within Python?

Cheers,

lsheng
  • 3,539
  • 10
  • 33
  • 43
  • What do you mean by "the body of the email should be a table"? Do you want an HTML-formatted version of your table? Excel is its own proprietary format, and can't -- as far as I know -- be embedded directly in an email. – Patrick Collins Jun 04 '14 at 02:47
  • @PatrickCollins I mean a table formatted as it is in Excel. Like grid lines, number formats, alignments etc. I havent learned HTML version? – lsheng Jun 04 '14 at 02:59
  • How would you do this in your normal email client? – Patrick Collins Jun 04 '14 at 03:00
  • @PatrickCollins I'd open the Excel, choose the formatted range, copy and paste to the message part of the email... – lsheng Jun 04 '14 at 03:08
  • That [implicitly converts the table to HTML, if your client supports it](http://superuser.com/questions/143228/embedding-an-excel-sheet-in-an-email). But email clients in general do no support sending arbitrary snippets of Excel tables in the body of a message. – Patrick Collins Jun 04 '14 at 03:11
  • 1
    @PatrickCollins I see. So I indeed need a HTML-formatted version of the original table. And I have to do it in Python using the win32com.client module, do you have any ideas? Thanks! – lsheng Jun 04 '14 at 03:15

2 Answers2

3

There are solutions regarding how to convert your Excel table to HTML here: How do I save Excel Sheet as HTML in Python?, and then you just drop the HTML into the body of your email.

Per request in the comments:

Once you have the HTML-formatted version of your table in a file called mytable.html, you can drop it into the email with: newMail.body = open("mytable.html").read()

Community
  • 1
  • 1
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
  • Thank you but the link saves the Excel in html, and I wanted the table to be directly pasted to the message body of the email. Not an attachment. – lsheng Jun 04 '14 at 03:32
  • Paste the HTML into the body of the email. – Patrick Collins Jun 04 '14 at 03:33
  • Say the file name is "myexcel.html", I cannot simply put newMail.body = myexcel.html since it only pastes the name of the file, other than the contents.. – lsheng Jun 04 '14 at 04:00
  • Glad to help. Consider upvoting and accepting this answer if you found it helpful. – Patrick Collins Jun 04 '14 at 06:23
  • tbh, can you put newMail.body = open("myexcel.html").read() in your answer so that other ppl can see it more clearly? – lsheng Jun 04 '14 at 06:29
  • 1
    newMail.body = open("mytable.html").read() doesn't work for me.It shows raw html code. But If I changed to newMail.HTMLbody. it says"this page uses frames, but your browser doesn't support them", how can I exactly paste excel to out email body anyway?frustrated. @PatrickCollins – Erik Johnsson Jun 07 '20 at 17:22
0

I m replying on old quest , but it comes on the top in search,may be helpful.

import win32com.client as win32
import pandas as pd
# Read A to D column and first 5 rows

df = pd.read_excel('yourfilepath.xlsx', index_col=False, nrows = 5,  usecols = "A:D")

body = df.to_html()

outlook = win32.gencache.EnsureDispatch('Outlook.Application')
new_mail = outlook.CreateItem(0)
new_mail.To = 'person@email.com'
new_mail.HTMLBody = (body)
new_mail.Send()
Hietsh Kumar
  • 1,197
  • 9
  • 17
  • Is there a way to send the excel contents with the color formatting. The above solution is working fine with unformatted data. – Harish P C Oct 10 '20 at 03:32