Creating and Viewing HTML files with Python - GeeksforGeeks

24 Jan, 2021
Python is one of the most versatile programming languages. It emphasizes code readability with extensive use of white space. It comes with the support of a vast collection of libraries which serve for various purposes, making our programming experience smoother and enjoyable. 
Python programs are used for:
Connecting with databases and performing backend development.
Making web applications.
Writing effective system scripts.
And especially in data science and artificial intelligence.
With this said, let us see how we can use python programs to generate HTML files as output. This is very effective for those programs which are automatically creating hyperlinks and graphic entities.
We will be storing HTML tags in a multi-line Python string and saving the contents to a new file. This file will be saved with a .html extension rather than a .txt extension.
Note: We would be omitting the standard <!DOCTYPE HTML> declaration!
Python3
# to open/create a new html file in the write modef = open('GFG.html', 'w')  # the html code which will go in the file GFG.htmlhtml_template = """<html><head><title>Title</title></head><body><h2>Welcome To GFG</h2>  <p>Default code has been loaded into the Editor.</p>  </body></html>"""  # writing the code into the filef.write(html_template)  # close the filef.close()
The above program will create an HTML file:
In order to display the HTML file as a python output, we will be using the codecs library. This library is used to open files which have a certain encoding. It takes a parameter encoding which makes it different from the built-in open() function.  The open() function does not contain any parameter to specify the file encoding, which most of the time makes it difficult for viewing files which are not ASCII but UTF-8.
Python3
# import moduleimport codecs  # to open/create a new html file in the write modef = open('GFG.html', 'w')  # the html code which will go in the file GFG.htmlhtml_template = """<html><head></head><body><p>Hello World! </p>  </body></html>"""  # writing the code into the filef.write(html_template)  # close the filef.close()  # viewing html files# below code creates a # codecs.StreamReaderWriter objectfile = codecs.open("GFG.html", 'r', "utf-8")  # using .read method to view the html # code from our objectprint(file.read())
Output:
In Python, webbrowser module provides a high-level interface which allows displaying Web-based documents to users. The webbrowser module can be used to launch a browser in a platform-independent manner as shown below:
Python3
# import moduleimport webbrowser  # open html filewebbrowser.open('GFG.html') 
Output:
True
Picked
python-utility
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
How to Install PIP on Windows ?
Enumerate() in Python
Read a file line by line in Python
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Reading and Writing to text files in Python
Python OOPs Concepts
Create a Pandas DataFrame from Lists