Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert MHTML to PNG in Python, open the MHTML file as a binary stream, create
ImageSaveOptions without a format argument, and call
Converter.convert_mhtml(). ImageSaveOptions() selects PNG by default.
MHTML stores HTML and related web resources in one archive. Converting MHTML to PNG creates lossless raster images of the rendered archive and is useful for previews, screenshots, visual comparisons, and workflows that require a static image instead of an editable web archive.
The basic conversion workflow is:
ImageSaveOptions() without a format argument to select PNG output.Converter.convert_mhtml(). 1import os
2import aspose.html.converters as conv
3import aspose.html.saving as sav
4
5input_dir = "data"
6output_dir = "output"
7os.makedirs(output_dir, exist_ok=True)
8
9input_path = os.path.join(input_dir, "document.mht")
10output_path = os.path.join(output_dir, "mhtml-page.png")
11
12# The parameterless constructor selects PNG by default
13options = sav.ImageSaveOptions()
14
15with open(input_path, "rb") as stream:
16 conv.Converter.convert_mhtml(stream, options, output_path)An MHTML archive can render to several PNG files when its content spans multiple output pages. Each file represents one rendered page; image conversion does not combine the complete archive into one tall image.
Use ImageSaveOptions when the PNG output requires a specific page size, margins, background color, resolution, or rendering settings. The following example configures the output page before converting MHTML to PNG:
ImageSaveOptions() for PNG output.page_setup.Converter.convert_mhtml() with the configured options and output path. 1import os
2import aspose.html.converters as conv
3import aspose.html.drawing as dr
4import aspose.html.saving as sav
5
6input_dir = "data"
7output_dir = "output"
8os.makedirs(output_dir, exist_ok=True)
9
10input_path = os.path.join(input_dir, "document.mht")
11output_path = os.path.join(output_dir, "mhtml-options.png")
12
13options = sav.ImageSaveOptions()
14options.page_setup.any_page = dr.Page(
15 dr.Size(800, 600),
16 dr.Margin(40, 40, 10, 10)
17)
18
19with open(input_path, "rb") as stream:
20 conv.Converter.convert_mhtml(stream, options, output_path)Configure only the options required by the output. The most relevant ImageSaveOptions properties for PNG conversion are:
| Property | What it controls |
|---|---|
| format | Output image format. The parameterless constructor selects PNG by default. |
| page_setup | Output page size, margins, and page layout. |
| horizontal_resolution and vertical_resolution | Resolution of output pages defined in physical units and internal raster images used during processing. |
| background_color | Color behind the rendered content. The default background is transparent. |
| use_antialiasing | Smoothing of rendered graphics. |
| text | Text rendering configuration. |
For additional page and rendering controls, see Fine-Tuning Converters.
PNG uses lossless compression, so saving the rendered page does not introduce JPEG-style compression artifacts. It is well suited to text, diagrams, interface elements, and other content that benefits from sharp edges. The result is still a raster image and does not retain editable HTML, CSS, or MHTML archive structure.
PNG supports transparency. Transparent areas can remain transparent when neither the archived page nor its CSS defines an opaque background. Set background_color only when the output requires a specific solid background.
| Issue | Explanation and fix |
|---|---|
| Conversion creates several PNG files | The rendered content spans multiple output pages. Adjust the page setup or source layout when a single page image is required. |
| Images or styles are missing | Check that the required resources are present in the MHTML archive and that its MIME parts and resource references are valid. |
| Content is clipped or surrounded by excessive space | Configure a page size and margins suitable for the archived page through page_setup. |
| The PNG background is opaque | Check background declarations in the archived HTML and CSS. They are rendered even though the default option background is transparent. |
MHTML content is paginated during rendering, while PNG is a single-page image format. The converter can therefore create one PNG file for each rendered output page.
PNG supports transparency, and the default ImageSaveOptions.background_color is transparent. Transparent output is possible when the archived HTML and CSS do not define an opaque page background.
Use the free online MHTML to PNG Converter for quick manual conversion without writing code. Use Aspose.HTML for Python via .NET when MHTML-to-PNG conversion must run programmatically in an application, service, or batch workflow.
Download complete examples and data files from GitHub.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.