Skip to main content

Parser for mhtml files

Project description

pimht

Python mhtml parser

Installation

$ pip install pimht

Example

import pimht

mhtml = pimht.from_filename("test.mhtml")
for part in mhtml:
    print(part.content_type, len(part.raw))

Modifying

import pimht

mhtml = pimht.from_filename("test.mhtml")
for part in mhtml.parts:
    if part.is_text:
        part.text = part.text.replace("Hello", "Goodbye")

with open("modified.mhtml", "wb") as f:
    f.write(mhtml.to_bytes())

API

Parsing (all return an MHTML object):

  • pimht.from_filename(path)
  • pimht.from_bytes(data)
  • pimht.from_string(text)
  • pimht.from_fileobj(fp) — text or binary mode

MHTML:

  • headers — top-level headers as a dict
  • parts — list of MHTMLPart (iterating the MHTML object yields the same parts)
  • to_bytes() — serialize the archive, including any modifications

MHTMLPart:

  • headers — part headers as a dict
  • content_type — mime type, e.g. "text/html"
  • is_text — whether content_type is text/*
  • charset — charset from the Content-Type header, or None
  • raw — decoded content as bytes; assignable
  • text — decoded content as str (text parts only); assignable

Assigning to raw or text re-encodes the content so to_bytes() reflects the change.

Performance

The chardet module, used by default, is slow. Performance can be improved by also installing faust-cchardet and pybase64 with:

$ pip install pimht[speedups]

This is aimed specifically at parsing Google Chrome generated snapshots as fast as possible, but feel free to report issues with MHTML files from other sources.

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

pimht-0.6.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

pimht-0.6.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file pimht-0.6.0.tar.gz.

File metadata

  • Download URL: pimht-0.6.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pimht-0.6.0.tar.gz
Algorithm Hash digest
SHA256 376cee5bd02526e65659446518d827065dd4fb88b2d710d57ac83eadd5eefa91
MD5 50b9402dd0d86cde4baf497fc3ce72d7
BLAKE2b-256 d49ce1f54623effc4dc7bf3bb4347aecd7edadcea3c63f5a99f206430a23a616

See more details on using hashes here.

Provenance

The following attestation bundles were made for pimht-0.6.0.tar.gz:

Publisher: publish-to-pypi.yml on pilate/pimht

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

File details

Details for the file pimht-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: pimht-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pimht-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7ad5b4328fdccdde15bf556ea8f8df1d08f818ce04aa77d6387bdb6700e3eac
MD5 0fc281fb9b11fc33da0d9274f75f4faa
BLAKE2b-256 ab54005e47f759629dc294eaa58b7bc34e84d452fbc67a695078ef9fe5bd8cf2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pimht-0.6.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on pilate/pimht

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