Thursday, September 25, 2025
HomeLanguagesHow to Convert HTML to Markdown in Python?

How to Convert HTML to Markdown in Python?

Markdown is a way of writing a formatted text on the web. This article discusses how an HTML text can be converted to Markdown. We can easily convert HTML to markdown using markdownify package. So let’s see how to download markdownify package and convert our HTML to markdown in python. 

Installation:

This module does not come in-built with Python. To install it type the below command in the terminal.

pip install markdownify

Approach 

  • Import module
  • Create HTML text
  • Use markdownify() function and pass the text to it
  • Display markdowned text

Example 1:

Python3




# import markdownify
import markdownify
  
  
# create html
html = """  
         <h1> <strong>Geeks</strong>
         for<br>
         Geeks
         </h1>
        """
  
# print HTML
print(html)
  
# convert html to markdown
h = markdownify.markdownify(html, heading_style="ATX")
  
print(h)


Output:

         <h1> <strong>Geeks</strong>
        for<br>
        Geeks
        </h1>
       
#  **Geeks**
for
Geeks

Example 2:

Python




# import markdownify
import markdownify
  
  
# create html
html = """  <h1>Fruits</h1>
         <ul>
             <li>  apple  </li>
             <li>  banana </li>
             <li>  orange </li>
        </ul>
          
        """
  
# print HTML
print(html)
  
# convert html to markdown
h = markdownify.markdownify(html, heading_style="ATX")
  
# print markdown
print(h)


Output:

<h1>Fruits</h1>
         <ul>
             <li>  apple  </li>
             <li>  banana </li>
             <li>  orange </li>
        </ul>
        
        
 # Fruits
*  apple 
*  banana 
*  orange 
RELATED ARTICLES

Most Popular

Dominic
32320 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6683 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6754 POSTS0 COMMENTS
Umr Jansen
6762 POSTS0 COMMENTS