Convert an MHTML Web Archive to PNG
Aspose.HTML for Python via .NET
converts MHTML or MHT archives to PNG with
Converter.convert_mhtml()
and
ImageSaveOptions
. Open the archive as a binary stream, select PNG output, and write the rendered image.
PNG uses lossless compression and supports transparency, which makes it suitable for text, diagrams, interface captures, and archived pages that need sharp edges. The final pixels depend on the configured canvas, resolution, CSS, fonts, background, and available resources.
Convert MHTML to PNG and View the Python Code
Choose an MHTML or MHT file and keep PNG selected. The widget creates PNG output and provides Python code for the same stream-based conversion.
{{#if_output 'PDF' 'DOCX' 'XPS'}}
import aspose.html.converters as conv
import aspose.html.saving as sav
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF'}}
import aspose.html.converters as conv
import aspose.html.rendering.image as rim
import aspose.html.saving as sav
{{/if_output}}
with open("{{input lower}}", "rb") as stream:
{{#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'}}
options = sav.ImageSaveOptions(rim.ImageFormat.{{output param2 upper}})
{{/if_output}}
conv.Converter.convert_mhtml(stream, options, "output.{{output lower}}")
The example opens input.mhtml as a binary stream, selects PNG explicitly, and writes output.png. Configure page and image settings when the required pixel dimensions or background differ from the defaults.
Python Code to Convert MHTML to PNG
import aspose.html.converters as conv
import aspose.html.rendering.image as rim
import aspose.html.saving as sav
with open("input.mhtml", "rb") as stream:
options = sav.ImageSaveOptions(rim.ImageFormat.PNG)
conv.Converter.convert_mhtml(stream, options, "output.png")
How to Convert MHTML to PNG in Python
- Install the
aspose-html-netpackage. - Open the MHTML or MHT file as a binary stream.
- Create
ImageSaveOptionswithImageFormat.PNG. - Configure page size, resolution, background color, CSS processing, or smoothing when required.
- Call
Converter.convert_mhtml()with the stream, options, and PNG path. - Close the input stream. The
withstatement handles this automatically.
Control PNG Size, Transparency, and Archived Resources
Use ImageSaveOptions to control the raster output without adding unnecessary settings:
- Page size: Set the rendered canvas dimensions for the intended use. Long content can produce more than one numbered PNG when it spans several pages.
- Resolution: Configure horizontal and vertical resolution when the output requires a different pixel density.
- Background: Keep transparency when it is useful, or set a background color for destinations that expect an opaque image.
- CSS and fonts: Apply the required supported styles and make the expected fonts available during conversion.
- Archived resources: Verify that the MHTML file contains the required images and styles or can access any external resources it references.
See
Convert MHTML to PNG in Python
and the
ImageSaveOptions API Reference
for available image settings.
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.
MHTML to PNG FAQ
1. Can MHTML be converted into one long PNG image?
Set a page size large enough for the laid-out archive when one PNG is required. If the content spans several rendered pages, the converter writes separate numbered PNG files instead of placing every page into one image.
2. Can a PNG created from MHTML have a transparent background?
Yes. PNG supports transparent pixels, and ImageSaveOptions uses a transparent background by default. A background declared by the archived HTML or CSS is still rendered, and background_color can be set when an opaque result is required.
3. Does MHTML to PNG conversion preserve selectable text and clickable links?
No. PNG stores pixels. Text is not selectable, and hyperlinks, forms, scripts, and other web interactions are not active in the image.
4. Do images and CSS need to be extracted from MHTML before conversion to PNG?
No. convert_mhtml() reads resources stored in the MHTML archive directly from the input stream. Resources that are only referenced by external URLs or local paths must still be accessible during conversion.
MHTML and PNG Conversion Guides
Choose another MHTML result or convert a different source to PNG.