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 4 in Python by ana1504.k
• 7,510 points
1,245 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

answered Feb 4 by SDeb
• 12,440 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,160 points
26 views
+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,160 points
112 views
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,160 points
1,043 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 134 views
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
• 12,440 points
44 views
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
• 12,440 points
60 views