Convert MHTML to DOCX – Python Code Examples

MHTML to DOCX conversion is often required to take advantage of DOCX format for specific tasks. Aspose.HTML for Python via .NET provides a simple and efficient way to perform this conversion using convert_mhtml() methods of the Converter class.

In this article, you find information on how to convert MHTML to DOCX using Aspose.HTML Python library and how to apply DocSaveOptions.

To continue following this tutorial, install and configure Aspose.HTML for Python via .NET in your Python project.

Online MHTML Converter

You can convert MHTML to DOCX with Aspose.HTML for Python via .NET API in real time. Load an MHTML file from a local file system, select the output format and run the example. The conversion will be performed with default save options. You will immediately receive the conversion result as a separate file.

                
            

Convert MHTML to DOCX using DocSaveOptions

Using convert_mhtml() methods is the most common way to convert MHTML into various formats. With Aspose.HTML for Python via .NET, you can convert MHTML to DOCX format programmatically with full control over a wide range of conversion parameters.

To convert MHTML to DOCX with DocSaveOptions specifying, you should follow a few steps:

  1. Open an existing MHTML file. In the example, we use the open() method to open and read MHTML from a file system at the specified path.
  2. Create an instance of the DocSaveOptions class. The DocSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting MHTML to DOCX format. In the example, we use the page_setup property that specifies the page size of the DOCX document, document_format, and css.media_type properties.
  3. Use one of the convert_mhtml() methods of the Converter class to save MHTML as a DOCX file. In the following example, the convert_mhtml() method takes the stream, options, output file path save_path and performs the conversion operation.

The following Python code example shows how to convert MHTML to DOCX using DocSaveOptions:

 1# Convert MHTML to DOCX using Python with custom settings
 2
 3import os
 4import aspose.html.converters as conv
 5import aspose.html.saving as sav
 6import aspose.html.drawing as dr
 7
 8# Setup directories and define paths
 9output_dir = "output/"
10input_dir = "data/"
11os.makedirs(output_dir, exist_ok=True)
12
13document_path = os.path.join(input_dir, "document.mht")
14save_path = os.path.join(output_dir, "document.docx")
15
16# Open an existing MHTML file for reading
17with open(document_path, "rb") as stream:
18
19    # Create an instance of DocSaveOptions
20    options = sav.DocSaveOptions()
21    options.page_setup.any_page.size = dr.Size(1000, 800)
22    options.document_format.DOCX
23    options.css.media_type.SCREEN
24
25    # Convert MHTML to DOCX
26    conv.Converter.convert_mhtml(stream, options, save_path)

Save Options – DocSaveOptions Class

Aspose.HTML for Python via .NET allows converting MHTML to DOCX using default or custom save options. DocSaveOptions class is configured to save the document as DOCX and it includes the following properties:

Some properties of this class inherit properties of base classes, such as DocRenderingOptions or RenderingOptions.

Download the Aspose.HTML for Python via .NET library to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.

Aspose.HTML offers a free online MHTML to DOCX Converter that converts MHTML to DOCX file with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!

Text “MHTML to DOCX Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.