Skip to main content

Intelligently pretty-print HTML/XML with inline tags.

Project description


While I love Beautiful Soup as a parser, BeautifulSoup.prettify() adds a linebreak between every tag. This results in unwanted white space between tags that should be inline, like <sup>, <a>, <span>, etc:

<p>Introducing GitHub<sup>&reg;</sup></p>

Introducing GitHub®


    Introducing GitHub

Introducing GitHub ®

This module parses HTML/XML as a raw string to more intelligently format tags.


You have two options:

  1. pip install prettierfier in your command line
  2. Copy the contents of to your own module.

This module is built with just the Python Standard Library and contains no external third-party dependencies.


prettify_xml(xml_string, indent=2, debug=False)

  • Can be used with no prior formatting.
        xml_string (str): XML text to prettify.
        indent (int, optional): Set size of XML tag indents.

    Test-only args:
        debug (bool, optional): Show results of each regexp application.

        str: Prettified XML.

prettify_html(html_string, debug=False)

  • Originally created to process BeautifulSoup.prettify() output.
  • Does not add or remove regular line breaks. Can be used with regular HTML if it already has the newlines you want to keep.
        html_string (str): HTML string to parse.

    Test-only args:
        debug (bool, optional): Show results of each regexp application.

        str: Prettified HTML.


import prettierfier

ugly_html = """<p>
    Introducing GitHub

pretty_html = prettierfier.prettify_html(ugly_html)

# Output
>>> <p>Introducing GitHub<sup>&reg;</sup></p>


Project details

Download files

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

Files for prettierfier, version 1.0.3
Filename, size File type Python version Upload date Hashes
Filename, size prettierfier-1.0.3.tar.gz (3.9 kB) File type Source Python version None Upload date Hashes View
Filename, size prettierfier-1.0.3-py3-none-any.whl (5.3 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page