HTML files contain Hypertext Markup Language (HTML), which is used to design and format the structure of a webpage. It is stored in a text format and contains tags that define the layout and content of the webpage. HTML files are widely used online and displayed in web browsers.
To preview HTML files, we make the use of browsers, like Google Chrome, Mozilla Firefox, Apple Safari etc. The task of creating and previewing HTML files can be automated with the help of python scripts.
Here are a few ways how we can open HTML files on chrome:
Method 1: Using os and webbrowser
The webbrowser module in python provides a high-level interface to allow displaying Web-based documents to users, and the os module provides a portable way of using operating system dependent functionalities (like reading or writing files, manipulating file paths etc.). So let’s see how a combination of these both can help us to open an HTML page in Chrome browser:
Function used: open_new_tab() function is used to open html file in a new tab of your default browser.
- Import module
- Open and Create file
- Add html code
- Write code to file
- Close file
- Open file in browser window
Method 2: Without using the ‘os’ module:
If the HTML file is in the same directory as that of the python script, then there is no need of defining the file path with the os module. We can simply run the html file in new browser using the given steps:
File in Use: GFG.html
- Create a html file that you want to open
- In Python, Import module
- Call html file using open_new_tab()
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course