Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
HTMLDocument, or a Url.MarkdownSaveOptions for custom conversion rules.Converter.convert_html().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.
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~~.
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.
The predefined option sets support different Markdown capabilities:
| Option set | Use it for | Important behavior |
|---|---|---|
MarkdownSaveOptions.default | Standard Markdown documents | Converts core structures such as headings, paragraphs, lists, links, images, emphasis, blockquotes, and code. Tables and strikethrough can remain as raw HTML. |
MarkdownSaveOptions.git | GitLab Flavored Markdown | Enables 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.
For custom conversion rules, MarkdownSaveOptions controls the feature set, formatting style, and related resources.
| Option | Use it to control |
|---|---|
| features | HTML element types converted to Markdown syntax. |
| formatter | Markdown formatting style, such as DEFAULT or GIT. |
| resource_handling_options | Handling of related resources such as images. |
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.
With the corresponding features enabled, links become [text](URL) and images become . 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.EMBEDEmbedding creates a self-contained Markdown file but increases its size, and the target Markdown renderer must allow data-URI images.
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.
Markdown preserves content structure rather than HTML presentation:
| HTML content | Conversion behavior |
|---|---|
| Headings, paragraphs, emphasis, lists, links, images, blockquotes, and code | Converted when the corresponding features are enabled. |
| Tables, strikethrough, and task lists | Converted by compatible extended features, including the GitLab preset. |
| Containers, SVG, and elements without a Markdown equivalent | Can remain as raw HTML. |
<style>, <script>, <link>, and <embed> | Do not produce equivalent Markdown syntax and can be omitted. |
| CSS formatting and page layout | Not preserved. |
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 feature | Features that can be processed inside |
|---|---|
| Header | Link, Emphasis, Strong, InlineCode, Image, Strikethrough, Video |
| Blockquote | Any |
| List | AutomaticParagraph, Link, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough, Video, TaskList, List |
| Link | Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough |
| AutomaticParagraph | Link, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough |
| Strikethrough | Link, Emphasis, Strong, InlineCode, Image, LineBreak |
| Table | Video, Strikethrough, Image, InlineCode, Emphasis, Strong, Link |
| Emphasis | Link, InlineCode, Image, LineBreak, Strikethrough, Video |
| Strong | Link, InlineCode, Image, LineBreak, Strikethrough, Video |
| Problem | Check or fix |
|---|---|
| A table remains as HTML | Use MarkdownSaveOptions.git or a compatible table feature and formatter. |
| Some elements remain as raw HTML | Enable the required feature or verify that the target renderer accepts raw HTML. |
| CSS styling is missing | Markdown does not preserve CSS-driven appearance or page layout. |
| Image references are broken | Supply a valid source location or base URI and keep the generated resource directory, or use ResourceHandling.EMBED. |
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.
Use MarkdownSaveOptions.git for compatible Markdown tables. CSS and page layout are not preserved because Markdown has no equivalent presentation model.
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.
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.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.