Outlook 2013 displays HTML instead of actual data

I am using win32com.client , python 2.7.x and outlook 2013 on windows platform.

I need to send the contents of an HTML file to the body of an outlook message.
I here, here and here on how to save excel as HTML and insert data into outlook .

However, when I read the file through win32com.client.Dispatch , instead of viewing the message, I see HTML code.

Here is the code that converts the processed xlsx file to HTML using win32.com .

 #Section to convert excel workbook to html myfile = os.getcwd()+ "\\" + outfile newfile = os.getcwd()+ "\\" + "emailData.html" xl = EnsureDispatch('Excel.Application') #xl.Visible = True wb3 = xl.Workbooks.Open(myfile) wb3WorkSheet = wb3.Worksheets(1) wb3WorkSheet.Activate() wb3.SaveAs(newfile, constants.xlHtml) wb3.Close(True) xl.Workbooks.Close() xl.Quit() del xl 

The above result is newfile , which is basically an export of an xlsx file saved as html. It then opens through the mail.body handler, which should read and display the actual content in Outlook.

Here is the code for this.

 from win32com.client.gencache import EnsureDispatch from win32com.client import constants, Dispatch #Create and open mail message def Emailer(text, subject, recipient): outlook = Dispatch('outlook.application') mail = outlook.CreateItem(0) mail.To = recipient mail.Subject = subject mail.HtmlBody = text #mail.HtmlBody = open(newfile).read() mail.body = open(newfile).read() attachment1 = os.getcwd()+"//"+outfile mail.Attachments.Add(attachment1) mail.Display(True) Emailer(pageTemplate, "test subject", " abc@yahoo.com " ) 

So, when I open newfile (html file) using mail.body = open(newfile).read() , it inserts html content into a new Outlook email item.

When I open newfile (html file) with mail.HtmlBody = open(newfile).read() , it gives the following error inside the outlook of the email body

ERROR: This page uses frames, but your browser doesn't support them.

Any ideas on this behavior?

Basically I want to copy / paste an HTML file (which is an xlsx export) in Outlook. Not sure if the above is the correct approach or other alternatives exist.

Is there a way to insert / render HTML frames into the body of an Outlook email?

Any pointers are appreciated.

+7
windows outlook win32com
source share
1 answer

You need to set the HTMLBody property, but keep in mind that HTML in Outlook is rendered by Word, not IE, and inline frames are not supported.

+1
source share

All Articles