Pythondex
Python Conversion Programs

Convert HTML To Image In Python

Last updated January 31, 2024 by Jarvis Silva

Want to know how to convert HTML to Image in Python then you are at the right place, Today in this article we will see how to use python to convert HTML File To Image.

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')

As you can see that the code is just 3 lines it because of the library we used which is html2image it allows us to convert html to image.

So you just have to install this library called html2image and you are good to go, to install use the below command.


pip install html2image

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.

I hope you found this tutorial helpful and you were able to convert html to image successfully, Do share this tutorial with your friends who might need this.

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 🙂