I have a Word document with formatted text, images, and tables. You can manually copy its contents and insert into Outlook with no problems. How to do it in Python?
My code:
import win32com.client
word = win32com.client.Dispatch("Word.Application")
doc = word.Documents.Open(your_doc_path)
contents = 'What here?' # doc.Content?
outlook = win32com.client.Dispatch("Outlook.Application")
# Create a new MailItem object
msg = outlook.CreateItem(0)
msg.Body = 'What here?' # `contents` throws pywintypes.com_error
msg.Display(False)
The closest problems:
Copy Word format into Outlook message
A word document's contents as the body of an email message
How to paste into Outlook from Microsoft Word
Exporting rich text to outlook and keep formatting
Does not work:
1-Saving a Word doc as HTML (analogously RTF) and
with open(html_path, 'r', errors='ignore') as f:
# Possible UnicodeDecodeError
doc_body = f.read()
msg.BodyFormat = 2 # olFormatHTML
msg.Body = doc_body
2-Reading document.xml in the zipped Word document.
Possible way:
The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body.
How to apply WordEditor via win32com?