Python Generate HTML from Markdown
Markdown is a lightweight markup language that lets you write documents in an easy-to-read, easy-to-write plain text format, which is then converted into structured HTML documents.
Markdown's syntax is simple and intuitive, and is 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 into HTML.
Steps to Convert Markdown to HTML
1. Install markdown library
First, we need to install Python's markdown library. We can use pip to install it:
pip install markdown
2. Write a Python Script
The following is a simple example that converts Markdown text into HTML:
Example
import
markdown
Define the Markdown text
md_text
=
"""
This is a heading
This is bold text.
This is italic text.
- List item 1
- List item 2
Click here to visit the website.
"""
Convert to HTML
html_output
=
markdown.
markdown
(
md_text
)
Output the HTML
(
html_output
)
The output is:
这是标题
这是 加粗 的文本。
这是 斜体 的文本。
- 列表项 1
- 列表项 2
点击这里 访问网站。
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) into an HTML file:
Code for convert_markdown_to_html.py:
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
)
(
"Markdown file has been successfully converted to an HTML file!"
)
3. Run the Script
Save the code above as convert_markdown_to_html.py, then run it in the terminal:
python convert_markdown_to_html.py
After running, theexample.md file will be converted to example.html files.
**Code Explanation:**
import markdown: This line imports the markdown library, which provides the functionality to convert Markdown text into 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 read its content into markdown_text variable.
html = markdown.markdown(markdown_text) : This line uses the markdown.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 example.html file.
Extended Features
markdown The library supports various 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 via Python is a simple yet powerful tool that can help you automate the documentation generation process. Whether you are writing blogs, documentation, or project notes, this approach can greatly improve your productivity. We hope this article helps you quickly get started with Markdown conversion in Python!