Convert HTML to an image in Python

0 votes
I want to convert following HTML to PNG image in Python. For example:

<html>
    <b>Bold text</b>
</html>
 

I have tried 'pisa' but it converts html to PDF, not to image. I can convert HTML to PDF and then convert PDF to PNG, but I was wondering if there is any direct solution.
Feb 5, 2019 in Python by ana1504.k
• 7,910 points
18,318 views

Ok so first install the pyfpdf library:

$ pip install pyfpdf

Then in your python script, store the html source code in a variable:

html_source= # html source code.

Import pyfpdf, add the below line at the beginning of your script:

import pyfpdf

Write the text to pdf:

pdf = pyfpdf.FPDF(format='letter')
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt=html_source align="C")
pdf.output("htmlsource.pdf")
Thnx bro! it worked
You're welcome :)
@SAK, I also suggest you sign up to the community so that you get regular updates on Python-related threads. Would be helpful for your projects and you can also help out others with queries.
Yeah sure, will do

1 answer to this question.

0 votes

webkit2png. The original version is OSX-only, but luckily there is a cross-platform fork:https://github.com/AdamN/python-webkit2png

Hope this helps!!

If you need to learn more about Python, It's recommended to join Python Certification Training today.

Thanks!

answered Feb 5, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
1 answer

I'm using Python 2.7 to convert an XML response (from a REST call to Atlassian Fisheye) into an HTML table.

You don't have a template matching tabularQueryResult in your ...READ MORE

answered Oct 4, 2018 in Python by Priyaj
• 58,060 points
1,420 views
0 votes
1 answer

How to save an image locally using Python in which we already know the URL address?

Hi, @Roshni, The following code is used to ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,850 points
1,895 views
0 votes
1 answer
0 votes
2 answers

How to add image file in html using python

Hi, I have implemented as below from robot.api import ...READ MORE

answered Feb 9, 2021 in Python by anonymous
10,687 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,403 views
0 votes
1 answer
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,300 points
934 views
0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 29, 2018 in Python by SDeb
• 13,300 points
823 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP