Creating and Viewing HTML files with Python
Last Updated :
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.
Creating an HTML file in python
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 mode
f = open('GFG.html', 'w')
# the html code which will go in the file GFG.html
html_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 file
f.write(html_template)
# close the file
f.close()
The above program will create an HTML file:

Viewing the HTML source 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 module
import codecs
# to open/create a new html file in the write mode
f = open('GFG.html', 'w')
# the html code which will go in the file GFG.html
html_template = """
<html>
<head></head>
<body>
<p>Hello World! </p>
</body>
</html>
"""
# writing the code into the file
f.write(html_template)
# close the file
f.close()
# viewing html files
# below code creates a
# codecs.StreamReaderWriter object
file = codecs.open("GFG.html", 'r', "utf-8")
# using .read method to view the html
# code from our object
print(file.read())
Output:

Viewing the HTML web file
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 module
import webbrowser
# open html file
webbrowser.open('GFG.html')
Output:
True
Similar Reads
How to Scrape Websites with Beautifulsoup and Python ? Have you ever wondered how much data is created on the internet every day, and what if you want to work with those data? Unfortunately, this data is not properly organized like some CSV or JSON file but fortunately, we can use web scraping to scrape the data from the internet and can use it accordin
10 min read
Web Scraping using lxml and XPath in Python Prerequisites: Introduction to Web Scraping In this article, we will discuss the lxml python library to scrape data from a webpage, which is built on top of the libxml2 XML parsing library written in C. When compared to other python web scraping libraries like BeautifulSoup and Selenium, the lxml pa
3 min read
How to write to an HTML file in Python ? Python language has great uses today in almost every field, it can be used along with other technologies to make our lives easier. One such use of python is getting the data output in an HTML file. We can save any amount of our input data into an HTML file in python using the following examples in t
2 min read
FastHTML: Revolutionizing Web Development with Python Web development is an ever-evolving field that requires strong knowledge of HTML-CSS, JavaScript, and front-end and back-end frameworks. FastHTML allows you to develop websites in pure Python. FastHTML is a project run by Jeremy Howard, aiming to make the process of developing web simpler by harness
4 min read
HTML Cleaning and Entity Conversion | Python The very important and always ignored task on web is the cleaning of text. Whenever one thinks to parse HTML, embedded Javascript and CSS is always avoided. The users are only interested in tags and text present on the webserver. lxml installation - It is a Python binding for C libraries - libxslt a
3 min read