I am trying to download an email from outlook sent-items. Currently I am able to save is '.msg' format. Is there anyway through which I can save the mail as '.html' or '.pdf' using python
from pathlib import Path
import win32com.client as win32
from datetime import date, timedelta
import os
import glob
# Create output folder
output_dir = Path.cwd()
output_dir.mkdir(parents=True, exist_ok=True)
# Connect to folder
outlook = win32.Dispatch('outlook.application').GetNamespace("MAPI")
# Connect to folder
sent_items = outlook.GetDefaultFolder(5)
# Get the required mail and store it locally
messages = sent_items.items
message = messages.GetLast()
name = str(message.subject)
message.saveas(os.getcwd()+'//'+name+".msg")
When I tried to replace .msg with .html or .pdf in the last line, then it is not working. The resultant file generated through html or pdf is displayed as special characters and not the actual .msg format
The Outlook object model doesn't provide any property or method for saving messages using the PDF file format. But you can use the OlSaveAsType enumeration for all available file formats. The HTML format (.html) is available. So, you just need to pass the
olHTMLvalue for the second parameter in addition to the file path:If you really need to save the message using the PDF file format you may consider using the Word object model for that. The Document.ExportAsFixedFormat2 method saves a document in PDF or XPS format. Use the GetInspector method to get the inspector where you may retrieve an instance of the Word
Documentobject which represents the message body. The Inspector.WordEditor property returns the Microsoft Word Document Object Model of the message being displayed. TheWordEditorproperty is only valid if theIsWordMailmethod returnstrueand theEditorTypeproperty isolEditorWord. The returned WordDocumentobject provides access to most of the Word object model