Python Markdown to HTML

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

Markdown's syntax is simple and intuitive, and it is often used for writing blogs, documents, README files, and more.

For more Markdown content, see:Markdown Tutorial

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


Steps to convert Markdown to HTML

1. Installationmarkdownlibrary

First, we need to install Python'smarkdownlibrary, you can usepipto install it:

pip install markdown

2. Write a Python script

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

Example

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.example.com) to visit the website.
"""


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

# Output HTML
print(html_output)

The output is:

<h1>这是标题</h1>
<p>这是 <strong>加粗</strong> 的文本。  
这是 <em>斜体</em> 的文本。</p>
<ul>
<li>列表项 1</li>
<li>列表项 2</li>
</ul>
<p><a href="../index.html">点击这里</a> 访问网站。</p>

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

convert_markdown_to_html.py file code:

import markdown

# Read 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 HTML to file
with open('example.html', 'w', encoding='utf-8') as file:
    file.write(html)

print("Markdown file successfully converted to HTML file!")

3. Run the script

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

python convert_markdown_to_html.py

After running,example.mdfile will be converted toexample.htmlfile.

Code explanation:

import markdown: This line of code imports themarkdownlibrary, 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 theopenfunction to open theexample.mdfile and read its contents into themarkdown_textvariable.

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

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

This code writes the converted HTML text to theexample.htmlfile.


Extended Features

markdownThe 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 as needed.

Converting Markdown to HTML with Python is a simple yet powerful tool that can help you automate the document generation process. Whether you are writing blogs, documents, or project descriptions, this approach can greatly improve your work efficiency. I hope this article helps you quickly get started with Markdown conversion in Python!


Reference Resources

Other Extensions