I am using win32com.client
, python 2.7.x
and outlook 2013
on windows
platform.
I need to post contents of a HTML
file to the body of outlook
message.
I followed the posts here ,here and here about how to save excel as HTML
and paste data within outlook
.
However , when I read the file via win32com.client.Dispatch
, instead of seeing message, I am seeing HTML
code.
Here is the code that converts a processed xlsx
file to html
format 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 output of above is newfile
which is basically an export of xlsx file saved as html. It is then opened via mail.body
handler which should read and display actual contents within outlook.
Here is the code for that.
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 pastes html content
within a new outlook email body.
When I open newfile
(html file) using mail.HtmlBody = open(newfile).read()
it's giving following error within outlook
email body
ERROR: This page uses frames, but your browser doesn't support them.
Any ideas on this behavior?
I basically want to copy/paste html
file (which is an export of xlsx
) within outlook email.
Not sure if above is correct approach or there are other alternatives.
Is there a way to paste /render HTML frames into outlook email body?
Any pointers is appreciated.