Convert HTML to an image in Python

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

    <b>Bold text</b>

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 4 in Python by ana1504.k
• 6,110 points

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.set_font("Arial", size=12)
pdf.cell(200, 10, txt=html_source align="C")
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.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

webkit2png. The original version is OSX-only, but luckily there is a cross-platform fork:

answered Feb 4 by SDeb
• 9,660 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
• 56,120 points
+4 votes
7 answers
+1 vote
1 answer

How to print an error in Python?

For Python 2.6 and later and Python ...READ MORE

answered Aug 23, 2018 in Python by Priyaj
• 56,120 points
0 votes
1 answer

How to resize image canvas to maintain square aspect ratio in Python, OpenCv

Building on Alexander-Reynolds answer above, here is ...READ MORE

answered Sep 4, 2018 in Python by Priyaj
• 56,120 points
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
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
• 9,660 points
0 votes
1 answer

How to Reverse a list in Python

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

answered Sep 28, 2018 in Python by SDeb
• 9,660 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.