Package: docx2msg
docx2msgConverts a docx to an Outlook Mail-Item with few lines of code.Descriptiondocx2msgis a python package that allows you to convert Microsoft Word .docx files to Outlook Mail-Item and .msg files. It provides a simple and efficient way to automate the conversion process, making it easier to automate with outlook email in your workflows.FeaturesConvert a docx to an Outlook Mail-Item just using Word and Outlook Application without any third-party library.Set mail properties from the header of the docx in YAML format.Able to use docx-template to render the docx body and set mail properties dynamically.RequirementsOS: WindowsApplication: Microsoft Word, Microsoft OutlookPython: 3.8+Python Packages: pywin32, python-docx, docx-template, pyyamlInstallationpipinstalldocx2msgUser GuideQuickstartThe example docx file are coming soon...Edit the body of the docx file to set the mail body as a normal docx file saving at "path/to/your/docx".You are recommand to edit the docx file with Microsoft Word Application in web layout mode to avoid the unexpected format issue.Edit the header of the docx file to set the mail properties. The header should be in YAML format in this way:Subject:Demo emailTo:anyone@example.comCC:p1@example.com;p2@example.comAttachments:path/to/your/attachment1.docx;path/to/your/attachment2.msgImportance:HighSensitivity:ConfidentialReadReceiptRequested:TrueCategories:RED CATEGORY, BLUE CATEGORYFlagRequest:Test FlagReminderTime:2024-02-29 14:00:00Convert a docx to an Outlook Mail-Item with few lines of code:importwin32com.clientfromdocx2msgimportDocx2Msgdocx_path=r"path/to/your/docx"withDocx2Msg(docx_path)asdocx:# set display=True to display the mail in Outlook Applicationmail=docx.convert(display=True)The mail will be displayed in Outlook Application and you can see the output.Advanced Usage with templateSincedocx2msgusesdocx-templateto render the docx body, you can use the same syntax to render the docx body and set mail properties dynamically.You can access thetemplateattribute of theDocx2Msgobject to utilize thedocx-templatefeatures.Go topython-docx-template’s documentationfor more details.Run the following code to convert a docx to an Outlook Mail-Item with a template:# the context to render the docxcontext={"name":"John Doe","age":30,"address":"123 Main St."}withDocx2Msg(docx_path)asdocx:# use template attribute to render the docx bodydocx.template.render(context)# convert the docx to an Outlook Mail-Itemmail=docx.convert()# display the mail in Outlook Applicationmail.Display()# save mail in draft foldermail.Save()# save mail as .msg filemail.SaveAs(r"path/to/your/output.msg")The output fromconvertmethod will be aMailItemobject, for further development, you can refer to theOutlook APIfor more details.Mail Headers SyntaxThe mail headers are in YAML format in the header of the docx file. The following properties are supported:PropertyTypeExampleTostr|list[str]anyone@example.comCCstr|list[str]p1@example.com;p2@example.comBCCstr|list[str]p1@example.com;p2@example.comSubjectstrDemo emailAttachmentsstr|list[str]path/to/your/file1.docx;path/to/your/file2.msgCategoriesstrRED CATEGORY, BLUE CATEGORYImportancestr|intHighSensitivitystr|intConfidentialReadReceiptRequestedboolTrueOriginatorDeliveryReportRequestedboolTrueFlagRequeststrFollow upVotingOptionsstrYes;NoReminderTimedatetime2024-02-29 14:00:00DeferredDeliveryTimedatetime2024-02-29 14:00:00ExpiryTimedatetime2024-02-29 14:00:00FlagDueBydatetime2024-02-29 14:00:00ReplyRecipientsstr|list[str]p1@example.com;p2@example.comSaveSentMessageFolderstr1/Auto/NewNote:For the some properties likeAttachments,To,CC,ReplyRecipientswhich may have a list of values, you can use the;to separate them.For theSaveSentMessageFolderproperty, the example "1/Auto/New" refers to the folder access byoutlook.Session.Folders[1].Folders["Auto"].Folder["New"]in python code, which is the sugar syntax for theSaveSentMessageFolderproperty.All the properties are vaild properties forOutlook.MailItemobject, so you can refer tohttps://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem#propertiesfor more details.API DocumentationThe API documentation is coming soon...Referencespywin32:https://pypi.org/project/pywin32/Outlook API:https://learn.microsoft.com/en-us/office/vba/api/overview/outlookWord API:https://learn.microsoft.com/en-us/office/vba/api/overview/worddocxtpl:http://docxtpl.readthedocs.org/jinja2:https://jinja.palletsprojects.com/en/3.0.x/