Last modified: Jan 10, 2023 By Alexander Williams

Python - Convert HTML to Markdown

Convert HTML to Markdown Using html2markdown

import html2markdown # 👉️ pip install html2markdown

# 👇 HTML Source
html = '''
<h1>How to Convert HTML to Markdown</h1>
<p>Use <strong>html2markdown</strong></p>
<h2>Test</h2>
<pre><code>import html2markdown</pre></code>
<h2>Test2</h2>
'''

convert = html2markdown.convert(html) # 👉️ Convert

print(convert)

Output:

# How to Convert HTML to Markdown

Use __html2markdown__

## Test

    import html2markdown

## Test2

Convert HTML to Markdown Using markdownify

import markdownify # 👉️ pip install markdownify

# 👇 HTML Source
html = '''
<h1>How to Convert HTML to Markdown</h1>
<p>Use <strong>html2markdown</strong></p>
<h2>Test</h2>
<pre><code>import html2markdown</pre></code>
<h2>Test2</h2>
'''

convert = markdownify.markdownify(html, heading_style="ATX") # 👉️ Convert

print(convert)

Output:

# How to Convert HTML to Markdown


Use **html2markdown**


## Test



```
import html2markdown
```

## Test2

Convert HTML file to Markdown

import markdownify # 👉️ pip install markdownify

html_file = "html.html" # 👉️ HTML Source File

markdown_file = "markdown.md" # 👉️ Markdown file to save

# 👇 Read
with open(html_file, "r") as f:
    convert = markdownify.markdownify(f.read(), heading_style="ATX") # 👉️ Convert HTML Source File

# 👇 Write
with open(markdown_file, "w") as f:
    f.write(convert) # 👉️ Write The response