Skip to content

Python Markdown to HTML

Markdown is a lightweight markup language that allows you to write documents in an easy-to-read and easy-to-write plain text format, and then convert them into structured HTML documents.

Markdown’s syntax is simple and intuitive, commonly used for writing blogs, documentation, README files, etc.

For more Markdown content, refer to: Markdown Tutorial

Python can use the markdown module to convert Markdown text to HTML.


First, we need to install Python’s markdown library. You can install it using pip:

pip install markdown

The following is a simple example that converts Markdown text to HTML:

import markdown

# Define Markdown text
md_text = """
# This is a Heading
This is **bold** text.
This is *italic* text.

- List item 1
- List item 2

[Click here](https://www.runoob.com) to visit the website.
"""

# Convert to HTML
html_output = markdown.markdown(md_text)

# Output HTML
print(html_output)

The output result is:

<h1>This is a Heading</h1>
<p>This is <strong>bold</strong> text.  
This is <em>italic</em> text.</p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
<p><a href="https://www.runoob.com">Click here</a> to visit the website.</p>

Next, we can write a simple Python script to convert a Markdown file (you can put the Markdown text from the example above into a file) to an HTML file:

import markdown

# Read the Markdown file
with open('example.md', 'r', encoding='utf-8') as file:
    markdown_text = file.read()

# Convert Markdown to HTML
html = markdown.markdown(markdown_text)

# Write the HTML to a file
with open('example.html', 'w', encoding='utf-8') as file:
    file.write(html)

print("Markdown file has been successfully converted to an HTML file!")

Save the above code as convert_markdown_to_html.py, then run it in the terminal:

python convert_markdown_to_html.py

After running, the example.md file will be converted to the example.html file.

Code Explanation:

import markdown: This line of code imports the markdown library, which provides the functionality to convert Markdown text to HTML.

with open('example.md', 'r', encoding='utf-8') as file:
    markdown_text = file.read()

This code uses the open function to open the example.md file and reads its content into the markdown_text variable.

html = markdown.markdown(markdown_text): This line of code uses the markdown.markdown() function to convert Markdown text to HTML text.

with open('example.html', 'w', encoding='utf-8') as file:
    file.write(html)

This code writes the converted HTML text to the example.html file.


The markdown library supports multiple extensions, such as tables, code highlighting, etc. You can enable extensions in the following way:

html = markdown.markdown(markdown_text, extensions=['tables', 'fenced_code'])

You can customize the style and structure of the HTML output according to your needs.

Converting Markdown to HTML through Python is a simple and powerful tool that can help you automate the document generation process. Whether writing blogs, documentation, or project descriptions, this method can greatly improve your work efficiency. We hope this article helps you quickly get started with the Markdown conversion feature in Python!