Package: markdownify
Installationpip install markdownifyUsageConvert some HTML to Markdown:frommarkdownifyimportmarkdownifyasmdmd('<b>Yay</b> <a href="http://github.com">GitHub</a>')# > '**Yay** [GitHub](http://github.com)'Specify tags to exclude:frommarkdownifyimportmarkdownifyasmdmd('<b>Yay</b> <a href="http://github.com">GitHub</a>',strip=['a'])# > '**Yay** GitHub'...or specify the tags you want to include:frommarkdownifyimportmarkdownifyasmdmd('<b>Yay</b> <a href="http://github.com">GitHub</a>',convert=['b'])# > '**Yay** GitHub'OptionsMarkdownify supports the following options:stripA list of tags to strip. This option can’t be used with theconvertoption.convertA list of tags to convert. This option can’t be used with thestripoption.autolinksA boolean indicating whether the “automatic link” style should be used when
aatag’s contents match its href. Defaults toTrue.default_titleA boolean to enable setting the title of a link to its href, if no title is
given. Defaults toFalse.heading_styleDefines how headings should be converted. Accepted values areATX,ATX_CLOSED,SETEXT, andUNDERLINED(which is an alias forSETEXT). Defaults toUNDERLINED.bulletsAn iterable (string, list, or tuple) of bullet styles to be used. If the
iterable only contains one item, it will be used regardless of how deeply
lists are nested. Otherwise, the bullet will alternate based on nesting
level. Defaults to'*+-'.strong_em_symbolIn markdown, both*and_are used to encodestrongoremphasizedtexts. Either of these symbols can be chosen by the optionsASTERISK(default) orUNDERSCORErespectively.sub_symbol, sup_symbolDefine the chars that surround<sub>and<sup>text. Defaults to an
empty string, because this is non-standard behavior. Could be something like~and^to result in~sub~and^sup^.newline_styleDefines the style of marking linebreaks (<br>) in markdown. The default
valueSPACESof this option will adopt the usual two spaces and a newline,
whileBACKSLASHwill convert a linebreak to\\n(a backslash and a
newline). While the latter convention is non-standard, it is commonly
preferred and supported by a lot of interpreters.code_languageDefines the language that should be assumed for all<pre>sections.
Useful, if all code on a page is in the same programming language and
should be annotated with```pythonor similar.
Defaults to''(empty string) and can be any string.code_language_callbackWhen the HTML code containspretags that in some way provide the code
language, for example as class, this callback can be used to extract the
language from the tag and prefix it to the convertedpretag.
The callback gets one single argument, an BeautifylSoup object, and returns
a string containing the code language, orNone.
An example to use the class name as code language could be:def callback(el):
    return el['class'][0] if el.has_attr('class') else NoneDefaults toNone.escape_asterisksIf set toFalse, do not escape*to\*in text.
Defaults toTrue.escape_underscoresIf set toFalse, do not escape_to\_in text.
Defaults toTrue.keep_inline_images_inImages are converted to their alt-text when the images are located inside
headlines or table cells. If some inline images should be converted to
markdown images instead, this option can be set to a list of parent tags
that should be allowed to contain inline images, for example['td'].
Defaults to an empty list.wrap, wrap_widthIfwrapis set toTrue, all text paragraphs are wrapped atwrap_widthcharacters. Defaults toFalseand80.
Use withnewline_style=BACKSLASHto keep line breaks in paragraphs.Options may be specified as kwargs to themarkdownifyfunction, or as a
nestedOptionsclass inMarkdownConvertersubclasses.Converting BeautifulSoup objectsfrommarkdownifyimportMarkdownConverter# Create shorthand method for conversiondefmd(soup,**options):returnMarkdownConverter(**options).convert_soup(soup)Creating Custom ConvertersIf you have a special usecase that calls for a special conversion, you can
always inherit fromMarkdownConverterand override the method you want to
change:frommarkdownifyimportMarkdownConverterclassImageBlockConverter(MarkdownConverter):"""
    Create a custom MarkdownConverter that adds two newlines after an image
    """defconvert_img(self,el,text,convert_as_inline):returnsuper().convert_img(el,text,convert_as_inline)+'\n\n'# Create shorthand method for conversiondefmd(html,**options):returnImageBlockConverter(**options).convert(html)Command Line InterfaceUsemarkdownify example.html > example.mdor pipe input from stdin
(cat example.html | markdownify > example.md).
Callmarkdownify-hto see all available options.
They are the same as listed above and take the same arguments.DevelopmentTo run tests and the linter runpip install toxonce, thentox.