Convert HTML to Markdown in Python

To convert HTML to Markdown in Python, pass an HTML file, HTMLDocument, or Url to Converter.convert_html() with MarkdownSaveOptions and an output path. Use MarkdownSaveOptions.default for standard Markdown or MarkdownSaveOptions.git for GitLab Flavored Markdown-compatible output.

HTML-to-Markdown conversion preserves supported document structure, such as headings, paragraphs, links, lists, emphasis, and code. It does not reproduce CSS-driven appearance or page layout. Elements without an equivalent Markdown representation can remain as raw HTML or be omitted.

Convert HTML to Markdown in Python

  1. Provide the HTML source as a file path, an HTMLDocument, or a Url.
  2. Choose the standard or GitLab preset, or create MarkdownSaveOptions for custom conversion rules.
  3. Pass the source, options, and output path to Converter.convert_html().
  4. Keep any generated resource files with the Markdown output, or configure resources to be embedded.

Convert an HTML File to Markdown

To convert a local HTML file, specify the source and output paths, select a Markdown preset, and call Converter.convert_html():

 1import os
 2import aspose.html.converters as conv
 3import aspose.html.saving as sav
 4
 5input_path = os.path.join("data", "document.html")
 6output_dir = "output"
 7os.makedirs(output_dir, exist_ok=True)
 8output_path = os.path.join(output_dir, "document.md")
 9
10conv.Converter.convert_html(
11    input_path,
12    sav.MarkdownSaveOptions.default,
13    output_path
14)

The standard preset converts common structures such as headings, paragraphs, lists, links, images, emphasis, blockquotes, and code.

Convert an HTML String to Markdown

The HTMLDocument constructor for an HTML string also accepts a base URI. It is the location used to resolve relative links and resource paths. The following self-contained example uses the current working directory instead of an unrelated placeholder website:

 1import os
 2import aspose.html as ah
 3import aspose.html.converters as conv
 4import aspose.html.saving as sav
 5
 6output_dir = "output"
 7os.makedirs(output_dir, exist_ok=True)
 8output_path = os.path.join(output_dir, "release-notes.md")
 9
10html = """
11<h1>Release Notes</h1>
12<table>
13  <tr><th>Version</th><th>Status</th></tr>
14  <tr><td>2.0</td><td>Current</td></tr>
15</table>
16<p><del>Obsolete option</del></p>
17"""
18
19base_uri = os.getcwd() + os.sep
20
21with ah.HTMLDocument(html, base_uri) as document:
22    conv.Converter.convert_html(
23        document,
24        sav.MarkdownSaveOptions.git,
25        output_path
26    )

Here, base_uri points to the current directory. If the HTML contains relative resources, replace it with the directory or webpage URL against which those references should be resolved. The GitLab preset converts the table to pipe-table syntax and <del> to ~~strikethrough~~.

Convert a Webpage URL to Markdown

Use a Url object to load an online page and convert its supported content:

 1import os
 2import aspose.html as ah
 3import aspose.html.converters as conv
 4import aspose.html.saving as sav
 5
 6output_dir = "output"
 7os.makedirs(output_dir, exist_ok=True)
 8output_path = os.path.join(output_dir, "aspose-from-url.md")
 9
10url = ah.Url("https://docs.aspose.com/html/files/aspose.html")
11conv.Converter.convert_html(
12    url,
13    sav.MarkdownSaveOptions.git,
14    output_path
15)

The application must have network access to the page and its linked resources. Markup without native Markdown syntax, such as some containers or SVG content, can remain as raw HTML.

Standard Markdown vs. GitLab Flavored Markdown

The predefined option sets support different Markdown capabilities:

Option setUse it forImportant behavior
MarkdownSaveOptions.defaultStandard Markdown documentsConverts core structures such as headings, paragraphs, lists, links, images, emphasis, blockquotes, and code. Tables and strikethrough can remain as raw HTML.
MarkdownSaveOptions.gitGitLab Flavored MarkdownEnables extended features such as Markdown tables, strikethrough, and task lists and applies the Git formatter.

The formatter property controls the formatting style, while features determines which element types are converted. Use the git preset when both the Git formatter and its compatible feature set are required.

Configure Markdown Output

For custom conversion rules, MarkdownSaveOptions controls the feature set, formatting style, and related resources.

OptionUse it to control
featuresHTML element types converted to Markdown syntax.
formatterMarkdown formatting style, such as DEFAULT or GIT.
resource_handling_optionsHandling of related resources such as images.

Select HTML Elements with MarkdownFeatures

Combine MarkdownFeatures flags with the bitwise OR operator when only selected element types should be converted:

 1import os
 2import aspose.html.converters as conv
 3import aspose.html.saving as sav
 4
 5input_path = os.path.join("data", "document.html")
 6output_dir = "output"
 7os.makedirs(output_dir, exist_ok=True)
 8output_path = os.path.join(output_dir, "selected-features.md")
 9
10options = sav.MarkdownSaveOptions()
11options.features = (
12    sav.MarkdownFeatures.LINK
13    | sav.MarkdownFeatures.AUTOMATIC_PARAGRAPH
14)
15
16conv.Converter.convert_html(input_path, options, output_path)

Only links and automatic paragraphs are converted to Markdown syntax. Other elements can remain as raw HTML; disabling a feature does not necessarily remove its content.

Preserve Links and Images

With the corresponding features enabled, links become [text](URL) and images become ![alt text](path). Local images can be saved to a companion resource directory. To embed them as data URIs, create the options and set the resource-handling rule before conversion:

1import aspose.html.saving as sav
2
3options = sav.MarkdownSaveOptions.git
4options.resource_handling_options.default = sav.ResourceHandling.EMBED

Embedding creates a self-contained Markdown file but increases its size, and the target Markdown renderer must allow data-URI images.

Inline HTML

Markdown processors can allow raw HTML in a Markdown document. To keep a supported element as HTML during conversion, mark it with markdown="inline", for example:

1<div markdown="inline"><code>keep_html()</code></div>

The marked <div> remains HTML instead of being converted to Markdown syntax. Raw HTML support and rendering also depend on the Markdown processor that reads the generated file.

HTML to Markdown Limitations

Markdown preserves content structure rather than HTML presentation:

HTML contentConversion behavior
Headings, paragraphs, emphasis, lists, links, images, blockquotes, and codeConverted when the corresponding features are enabled.
Tables, strikethrough, and task listsConverted by compatible extended features, including the GitLab preset.
Containers, SVG, and elements without a Markdown equivalentCan remain as raw HTML.
<style>, <script>, <link>, and <embed>Do not produce equivalent Markdown syntax and can be omitted.
CSS formatting and page layoutNot preserved.

Feature Nesting

Not every Markdown feature can be nested inside every other feature. For example, list elements inside table elements are not converted to native Markdown. The following combinations are supported for members of MarkdownFeatures:

Parent featureFeatures that can be processed inside
HeaderLink, Emphasis, Strong, InlineCode, Image, Strikethrough, Video
BlockquoteAny
ListAutomaticParagraph, Link, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough, Video, TaskList, List
LinkEmphasis, Strong, InlineCode, Image, LineBreak, Strikethrough
AutomaticParagraphLink, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough
StrikethroughLink, Emphasis, Strong, InlineCode, Image, LineBreak
TableVideo, Strikethrough, Image, InlineCode, Emphasis, Strong, Link
EmphasisLink, InlineCode, Image, LineBreak, Strikethrough, Video
StrongLink, InlineCode, Image, LineBreak, Strikethrough, Video

Common HTML to Markdown Conversion Issues

ProblemCheck or fix
A table remains as HTMLUse MarkdownSaveOptions.git or a compatible table feature and formatter.
Some elements remain as raw HTMLEnable the required feature or verify that the target renderer accepts raw HTML.
CSS styling is missingMarkdown does not preserve CSS-driven appearance or page layout.
Image references are brokenSupply a valid source location or base URI and keep the generated resource directory, or use ResourceHandling.EMBED.

Related Markdown Workflows

Other Platforms

FAQ

Can I convert an HTML string to Markdown?

Yes. Create an HTMLDocument from the string and a meaningful base URI, then pass it to Converter.convert_html(). The base URI resolves relative links and resources.

Does HTML-to-Markdown conversion preserve tables and CSS?

Use MarkdownSaveOptions.git for compatible Markdown tables. CSS and page layout are not preserved because Markdown has no equivalent presentation model.

Can I convert HTML to Markdown for free?

Use the free online converter below for occasional manual conversion. For programmatic conversion, evaluate the library or follow the licensing guide to request a temporary license.

Try Online HTML to Markdown Conversion

                
            

Use the free online HTML to Markdown Converter for quick manual conversion without writing code. Use Aspose.HTML for Python via .NET when HTML-to-Markdown conversion must run in a Python application, documentation pipeline, or content-migration workflow.

Free Online HTML to Markdown Converter