Explore

Close
Python Programs Share

Convert HTML To Image In Python

Convert HTML To Image In Python

Looking for a tutorial on how to convert HTML to Image In Python then you are at the right place, Today in this article I will show you how to convert HTML File To Image using python programming.

HTML is a markup language which is used to build websites, So you have a html file and you want to convert it into an image file, then you can do it in python easily with just 3 lines of code.

Python Code To Convert HTML To Image


# Installing and importing html2image library
from html2image import Html2Image

# Intializing the module
htmlImg = Html2Image()

# In html_file enter html file name you want to convert
# And at save_as enter the name you want of the image
htmlImg.screenshot(html_file='sample.html',save_as='test.png')

Above is the python code to convert HTML to Image file. You can run this code and see the result. To run this program you need to have python installed, if you don’t have, then read this: Install and setup python.

Follow this steps to run this program:

  • First, create a new folder for this program.
  • Open it in a code editor of your choice.
  • Create a python file with .py extension.
  • Copy and paste the above code in the file

Now you have the code but don’t run it because it will give error because as you can see we have used a library called html2image to convert so we need to install it use below command to install


pip install html2image

The above command will install the library on your computer, Now you are ready to run this program so to run it open a command prompt or terminal at this program folder location and paste the below command


python filename.py

After running the program you will see that a new image file has been created with the name you have given so if you open the image then you will see that the image is the html output.

Summary

This was the tutorial on converting a HTML To Image in python. I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might need this or find it helpful.

Here are some more python programs you will find helpful:

I hope you found what you were looking for from this python tutorial, and if you want more python tutorials like this, do join our Telegram channel to get updated.

Thanks for reading, have a nice day 🙂