Skip to main content

MHTML to HTML converter library using Python stdlib

Project description

unmhtml

PyPI version

Convert MHTML files to standalone HTML with embedded CSS and resources.

Installation

pip install unmhtml

Usage

from unmhtml import MHTMLConverter

# Convert MHTML file to HTML (secure by default)
converter = MHTMLConverter()
html_content = converter.convert_file('saved_page.mhtml')

# Save as standalone HTML
with open('output.html', 'w') as f:
    f.write(html_content)

# Unsafe conversion preserving original content
unsafe_converter = MHTMLConverter(
    remove_javascript=False,
    sanitize_css=False,
    remove_forms=False,
    remove_meta_redirects=False
)
html_content = unsafe_converter.convert_file('trusted_page.mhtml')

Features

  • Pure Python - No external dependencies, uses only standard library
  • Standalone HTML - Embeds CSS and converts resources to data URIs
  • Secure by Default - All security sanitization enabled by default for safe processing
  • Graceful degradation - Handles malformed MHTML files
  • Memory efficient - Processes large files without excessive memory usage

Security

The library is secure by default - all security features are enabled automatically to safely display untrusted content:

Available Security Options (All Enabled by Default)

  • remove_javascript=True - Removes <script> tags, event handlers (onclick, onload, etc.), and converts javascript: URLs to safe # anchors
  • sanitize_css=True - Removes external CSS URLs (http://, https://, //, /absolute) while preserving relative URLs (image.png, ../fonts/font.woff) and data URIs
  • remove_forms=True - Removes form elements (<form>, <input>, <textarea>, <select>) that could submit data externally
  • remove_meta_redirects=True - Removes dangerous meta tags (refresh redirects, set-cookie, dns-prefetch) that could be used maliciously

Usage Examples

# Default secure conversion (all security features enabled)
converter = MHTMLConverter()
html_content = converter.convert_file('untrusted.mhtml')

# Preserve original JavaScript (unsafe - only for trusted content)
javascript_converter = MHTMLConverter(remove_javascript=False)
html_content = javascript_converter.convert_file('trusted.mhtml')

# Completely unsafe conversion (preserve all original content)
unsafe_converter = MHTMLConverter(
    remove_javascript=False,
    sanitize_css=False,
    remove_forms=False,
    remove_meta_redirects=False
)
html_content = unsafe_converter.convert_file('trusted.mhtml')

Requirements

  • Python 3.8+

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unmhtml-0.2.0.tar.gz (55.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unmhtml-0.2.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file unmhtml-0.2.0.tar.gz.

File metadata

  • Download URL: unmhtml-0.2.0.tar.gz
  • Upload date:
  • Size: 55.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for unmhtml-0.2.0.tar.gz
Algorithm Hash digest
SHA256 48df3a83aafd2af7e20f2bdde4ae335771b85384302f86daea53c6baf0f88b8c
MD5 73a938d00966972a5d46377453d959eb
BLAKE2b-256 865fe4afd463b3bc0378b747ae83af196123c56d9b0ed648bd5c836a8a2408f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for unmhtml-0.2.0.tar.gz:

Publisher: publish.yml on resolve-works/unmhtml

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file unmhtml-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: unmhtml-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for unmhtml-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cc6c212cbdb87fb9e6b77b1c635accf3516ba12cd92441c97a7ee7c4910d0cc5
MD5 0b5c70a9e5e803b5ff5303527fd78197
BLAKE2b-256 4a9677a68362a1d91a0ad7d767e8cabfe4524aa48866dfe8a90a862b13895a2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for unmhtml-0.2.0-py3-none-any.whl:

Publisher: publish.yml on resolve-works/unmhtml

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page