Skip to main content

Convert a docx to an Outlook Mail-Item.

Project description

docx2msg

Converts a docx to an Outlook Mail-Item with few lines of code.

Description

docx2msg is 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 work with outlook email in your workflows.

Features

  • Convert 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.

Installation

pip install docx2msg

Usage

Simple Usage

  1. 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.

  2. Edit the header of the docx file to set the mail properties. The header should be in YAML format in this way:

    Subject: Demo email
    To: anyone@example.com
    CC: p1@example.com;p2@example.com
    Attachments: path\to\your\attachment1.docx;path\to\your\attachment2.msg
    Importance: High
    Sensitivity: Confidential
    ReadReceiptRequested: True
    Categories: RED CATEGORY, BLUE CATEGORY
    FlagRequest: Test Flag
    ReminderTime: 2024-02-29 14:00:00
    
  3. Convert a docx to an Outlook Mail-Item with few lines of code:

    import win32com.client
    from docx2msg import Docx2Msg
    
    outlook = win32com.client.Dispatch("Outlook.Application")
    word = win32com.client.Dispatch("Word.Application")
    docx_path = r"path\to\your\docx"
    with Docx2Msg(docx_path, outlook=outlook, word=word) as docx:
        # set display=True to display the mail in Outlook Application
        mail = docx.convert(display=True)
    
  4. The mail will be displayed in Outlook Application and you can see the output.

Advanced Usage with template

Since docx2msg uses docx-template to render the docx body, you can use the same syntax to render the docx body and set mail properties dynamically.

You can access the template attribute of the Docx2Msg object to utilize the docx-template features.

Go to python-docx-template’s documentation for more details.

Run the following code to convert a docx to an Outlook Mail-Item with a template:

# the context to render the docx
context = {
    "name": "John Doe",
    "age": 30,
    "address": "123 Main St."
}
with Docx2Msg(docx_path, outlook=outlook, word=word) as docx:
    # use template attribute to render the docx body
    docx.template.render(context)
    # convert the docx to an Outlook Mail-Item
    mail = docx.convert()
    # display the mail in Outlook Application
    mail.Display()
    # save mail in draft folder
    mail.Save()
    # save mail as .msg file
    mail.SaveAs(r"path\to\your\output.msg")

The output from convert method will be an MailItem object, for further development, you can refer to the Outlook API for more details.

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docx2msg-0.1.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

docx2msg-0.1.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file docx2msg-0.1.1.tar.gz.

File metadata

  • Download URL: docx2msg-0.1.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.31.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for docx2msg-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c1cf21dc93540ce947b66e842cd85d140b4e94486900c38fdea366ade91dbf8a
MD5 9b571acad5cd7baafe1ffdda16f2d584
BLAKE2b-256 2629a683f40dff878fdd77eae0b423e9c8f7faac8e1f54e7dff417420ff74c23

See more details on using hashes here.

File details

Details for the file docx2msg-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: docx2msg-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.31.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for docx2msg-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c27f68c00a0b882b6942245ec22e5d9decb9d6bc185ee1c751d6737aa183c31d
MD5 01cc02d9468702ec2e573845f19f149f
BLAKE2b-256 d13aab32fd2bbf869f42bd57c3ee30d4c4d09cd301449b16554c50a5bff84ff7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page