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.
{{#if_output 'PDF' 'DOCX' 'XPS' 'MHTML' 'MD' }}
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP'}}
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.rendering.image as rim
import aspose.html.saving as sav
{{/if_output}}
{{#if_output 'XHTML'}}
import aspose.html as ah
import aspose.html.saving as sav
{{/if_output}}
with ah.HTMLDocument("{{input lower}}") as document:
{{#if_output 'PDF'}}
options = sav.PdfSaveOptions()
{{/if_output}}
{{#if_output 'DOCX'}}
options = sav.DocSaveOptions()
{{/if_output}}
{{#if_output 'XPS'}}
options = sav.XpsSaveOptions()
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP'}}
options = sav.ImageSaveOptions(rim.ImageFormat.{{output param2 upper}})
{{/if_output}}
{{#if_output 'MHTML'}}
options = sav.MHTMLSaveOptions()
{{/if_output}}
{{#if_output 'MD'}}
options = sav.MarkdownSaveOptions.default
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP' 'PDF' 'MHTML' 'MD' 'XPS' 'DOCX'}}
conv.Converter.convert_html(document, options, "output.{{output lower}}")
{{/if_output}}
{{#if_output 'XHTML'}}
options = sav.HTMLSaveOptions()
options.document_type = sav.HTMLSaveOptions.XHTML
document.save("output.xhtml", options)
{{/if_output}}
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
- Install the
aspose-html-netpackage. - Load an HTML file, string, document, or webpage URL. Supply a base URI when HTML text contains relative resource references.
- Create an
MHTMLSaveOptionsinstance. - Configure resource handling, URL restrictions, and linked-page depth when the defaults do not match the archive scope.
- Call
Converter.convert_html()with the source, options, and.mhtmloutput 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
defaultrule controls CSS, images, fonts, and other general resources. Thejava_scriptrule controls external scripts separately. Both useSAVEby default. - Resource URL scope:
resource_url_restrictioncontrols which resource URLs may be processed. Its default isSAME_HOST, so cross-host CDN resources can require an intentionally broader rule. - Linked-page scope:
page_url_restrictionlimits other HTML pages reached through links. Its default isROOT_AND_SUB_FOLDERS. - Linked-page depth:
max_handling_depthis0by default, so linked HTML pages are not included. A value of1includes 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_restrictionfor 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.