convert markdown to html in python

How to Convert Markdown to HTML in Python

Markdown is a minimal markup language that can be created and edited using any text editor. Sometimes you may need to convert Markdown to HTML. You can do this using many programming languages as well as third-party software. In this article, we will learn how to convert Markdown to HTML in Python.


How to Convert Markdown to HTML in Python

We will be using python-markdown library to convert markdown language to html. We will not only convert markdown to HTML but also generate table of contents.


1. Install Markdown library

Open terminal and run the following command to install markdown library using pip.

$ python -m pip install markdown


2. Convert Markdown text to HTML via python script

Here is a simple python script to convert markdown text to HTML.

import markdown

# Simple conversion in memory
md_text = '# Hello\n\n**Text**'
html = markdown.markdown(md_text)
print(html)

Here is a simple script to convert markdown file to HTML using python library.

import markdown

markdown.markdownFromFile(
    input='/home/data/input.md',
    output='/home/data/output.html',
    encoding='utf8',
)

Please make sure to replace /home/data/input.md with the full path of input file, and /home/data/output.html with the full path of target HTML file.


3. Convert Markdown to HTML via Command Line

You can also convert Markdown to HTML via command line, using markdown library. Here is the command to convert markdown input file into HTML.

# Convert from a file
python -m markdown /home/data/input.md

Here is the command to convert markdown file into HTML using standard input.

# Convert using STDIN/STDOUT
cat /home/data/input.md | python -m markdown > /home/data/output.html


4. Generate Table of Contents (TOC)

Python markdown library comes with many extensions. One such extension is the function to create table of contents. Here is a simple code to create table of contents. In this case, you need to add [TOC] string at the beginning of your markdown to be converted into HTML.

import markdown

md_text = '[TOC]\n# Title\n**text**'
html = markdown.markdown(md_text, extensions=['toc'])
print(html)

Similarly, you can also convert markdown to fenced code. In this case, you need to add triple backticks at the beginning and end of your markdown which you want to be fenced.

import markdown

md_text = """
# Title

```python
# some code block
```
"""
html = markdown.markdown(md_text, extensions=['fenced_code'])
print(html)

If you want to highlight code syntax you can do so with codehilite extension. But it requires pygments library to run. So we install it with the following command.

$ python -m pip install pygments

Here is an example to add code fencing and syntax highlighting to our HTML.

import markdown

md_text = """
```python hl_lines="1 3"
# some Python code
hi = 'Hello'
print(hi)
```
"""
html = markdown.markdown(md_text, extensions=['fenced_code', 'codehilite'])
print(html)

There are many more extensions that you can use to customize your HTML. In this article, we have learnt how to convert markdown to HTML via python script or command line. We have also seen how to use some of the markdown extensions.

Also read:

Python Delete Dictionary key in Iteration
Python Remove Item From List While Iterating
How to Rename File Using Python
How to Uninstall Java in Ubuntu
How to Format USB Drives in Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *