Convert HTML and Related Resources to MHTML

Aspose.HTML for Python via .NET converts an HTML file, string, document, or URL to MHTML with Converter.convert_html() and MHTMLSaveOptions . MHTML stores HTML markup and handled related resources as MIME parts in one .mht or .mhtml web archive.

The archive can package accessible CSS, images, fonts, scripts, and other handled resources required by the current page. It preserves web content rather than converting the page into a fixed-layout PDF or a screenshot.


Test HTML to MHTML and View the Python Code

Choose an HTML file or webpage URL, then keep MHTML selected to create a web archive. The widget provides matching Python code for the selected source and output format.

The default example converts document.html to output.mhtml with MHTMLSaveOptions. The source location supplies context for relative resources. Configure URL restrictions and linked-page depth when the archive must include content outside the default scope.

Python Code to Convert HTML to MHTML

import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav

with ah.HTMLDocument("document.html") as document:
    options = sav.MHTMLSaveOptions()
    conv.Converter.convert_html(document, options, "output.mhtml")



How to Convert HTML to MHTML in Python

  1. Install the aspose-html-net package.
  2. Load an HTML file, string, document, or webpage URL. Supply a base URI when HTML text contains relative resource references.
  3. Create an MHTMLSaveOptions instance.
  4. Configure resource handling, URL restrictions, and linked-page depth when the defaults do not match the archive scope.
  5. Call Converter.convert_html() with the source, options, and .mhtml output path.



Control MHTML Resources and Linked Pages

MHTMLSaveOptions.resource_handling_options separates resources required by the current page from other HTML pages reached through links:

  • Current-page resources: The default rule controls CSS, images, fonts, and other general resources. The java_script rule controls external scripts separately. Both use SAVE by default.
  • Resource URL scope: resource_url_restriction controls which resource URLs may be processed. Its default is SAME_HOST, so cross-host CDN resources can require an intentionally broader rule.
  • Linked-page scope: page_url_restriction limits other HTML pages reached through links. Its default is ROOT_AND_SUB_FOLDERS.
  • Linked-page depth: max_handling_depth is 0 by default, so linked HTML pages are not included. A value of 1 includes directly linked pages. This setting does not control the CSS and images used by the current page.
  • Source location: Provide a base URI for HTML strings, and keep local files available relative to the source document.
  • Remote resources: Verify network access and review resource_url_restriction for cross-host CDN files. Resources that require an unavailable authentication flow cannot be packaged automatically.
  • Archive size: Broader URL restrictions and linked-page depth can increase the number and size of MIME parts in the output.

Use URL restrictions and depth together. Avoid unlimited linked-page processing for untrusted or uncontrolled websites because it can retrieve an unexpectedly large set of pages and resources.

See Convert HTML to MHTML in Python and ResourceHandlingOptions for the documented defaults and scope controls.



Install Aspose.HTML for Python via .NET

Install the aspose-html-net package from PyPI .

Install Aspose.HTML for Python via .NET

pip install aspose-html-net

Review the installation guide and system requirements before deployment. Request a temporary license to test the API without evaluation limitations.



HTML to MHTML FAQ

1. Are MHT and MHTML the same web archive format?

Yes. .mht and .mhtml are commonly used filename extensions for the MIME HTML web archive format. Use the extension expected by the receiving application or storage workflow.

2. Can an MHTML file be edited with HTMLDocument?

The HTMLDocument class is intended for HTML and XHTML DOM work, not direct MHTML archive editing. Modify the source HTML document before conversion, then create a new MHTML archive from that updated source.

3. Does HTML to MHTML conversion preserve user interaction and live server state?

No. MHTML stores handled markup and resources. It does not record button clicks, submitted form state, authenticated server sessions, or content that becomes available only through later user interaction.

4. Will an MHTML archive look identical in every browser or viewer?

Not necessarily. MHTML support and page presentation can vary between applications. Test the generated archive in the browser, mail client, or document system that will open it.

MHTML and Web Archive Conversion Guides

Continue with another HTML output or convert an existing MHTML archive to a document or image.