Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (
Help us improve Python packaging - Donate today!

Conservatively convert html to markdown

Project Description

Experimental: I have only tested this with the Python markdown module, and under relatively limited circumstances.

Purpose: Converts html to markdown while preserving unsupported html markup. The goal is to generate markdown that can be converted back into html. This is the major difference between html2markdown and html2text. The latter doesn’t purport to necessarily be reversible.

Usage example

import html2markdown
print html2markdown.convert('<h2>Test</h2><pre><code>Here is some code</code></pre>')


## Test

    Here is some code

Information and caveats

Does not convert the content of block-type tags other than <p> – such as <div> tags – into Markdown

It does convert to markdown the content of inline-type tags, e.g. <span>.

Input: <div>this is stuff. <strong>stuff</strong></div>

Result: <div>this is stuff. <strong>stuff</strong></div>

Input: <p>this is stuff. <strong>stuff</strong></p>

Result: this is stuff. __stuff__ (surrounded by a newline on either side)

Input: <span style="text-decoration:line-through;">strike <strong>through</strong> some text</span> here

Result: <span style="text-decoration:line-through;">strike __through__ some text</span> here

Except in unprocessed block-type tags, formatting characters are escaped

Input: <p>**escape me?**</p> (in html, we would use <strong> here)

Result: \*\*escape me?\*\*

Input: <span>**escape me?**</span>

Result: <span>\*\*escape me?\*\*</span>

Input: <div>**escape me?**</div>

Result: <div>**escape me?**</div> (block-type)

Attributes not supported by Markdown are kept

Example: <a href="http://myaddress" title="click me"><strong>link</strong></a>

Result: [__link__](http://myaddress "click me")

Example: <a onclick="javascript:dostuff()" href="http://myaddress" title="click me"><strong>link</strong></a>

Result: <a onclick="javascript:dostuff()" href="http://myaddress" title="click me">__link__</a> (the attribute onclick is not supported, so the tag is left alone)


  • Currently, only underlines (_) and asterisks (*) are escaped.
  • Tables are kept as html.

Release History

This version
History Node


History Node


History Node


History Node


History Node


Download Files

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

File Name & Hash SHA256 Hash Help Version File Type Upload Date
(4.2 kB) Copy SHA256 Hash SHA256
Source Feb 27, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting