Change language

reStructuredText | .rst file to HTML file using Python for documentation

reStructuredText — it is a file format for text data that is mainly used by Python-based communities to develop documentation in a simple way similar to other tools like Javadoc for Java. Most Python-based software and library documents are written using reStructuredText, and therefore important to study when working in any organization.

Like Python, RST syntax is also sensitive to indentation.

Sample reStructuredText code:
Some basic HTML syntax and rendering are shown below.

gfg
usage syntax HTML rendering
Heading *****
Heading
*****

Heading

italic * italic * italic
bold ** bold ** bold
link ’gfg "python.engineering" ’
verbatim ’’ Some text or code’’

 Some text or code 

restructured.rst

************************* *****
This is example of rst on GFG
****** *************************

 
* Python.Engineering in italic *

 
** Python.Engineering in bold **

 
’Gfg website "www. python.engineering" ’

  
’’ Python.Engineering in vebatim’’

Save the file with the extension .rst .

Python code:

Below is the Python code for converting files. rst to HTML files.  docutils — it is a predefined library loaded during Python installation. The publish_file method is used to convert the first to HTML, passing the filenames as parameters.

import docutils.core

 
docutils.core.publish_file (

source_path = "restructured.rst" ,

destination_path = " Output.html " ,

writer_name = " html " )

output.html
After running the Python code, the HTML file will be generated in the same directory as as the first file. HTML is more complex than normal human-written HTML because it is generated automatically. The following image shows the HTML rendering of the reStructuredText shown above.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically