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 5, 2019 in Python by ana1504.k
• 7,910 points
can you help me in converting html source code generated from selenium to PDF?

Hey @SAK!

You can use the pdfkit library to convert the html code to PDF. 

Firsty, you'll have to install wkhtmltopdf and pdfkit for this:

$ sudo apt-get install wkhtmltopdf
$ pip install pdfkit

Next, to convert the code to PDF, refer to the following Python script:

import pdfkit

#If the source code is a string, then use this;
pdfkit.from_string('<string>', '<Output filename with .pdf as extension>')

#If the source code is a html file, then use this:
pdfkit.from_file('<html filename>','<Output filename with .pdf as extension>')
Hi Omkar,

The problem here is I'm doing this work Online where i just have option to use Python package... i cant use wkhtmltopdf there... is there any other way?

Can you try using the script without installing wkhtmltopdf? Check if it works, or post the error that you get. 

"OSError: No wkhtmltopdf executable found: "b''" If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf -"

This is the error i get if i just use pdfkit without wkhtmltopdf

Okay, That approach won't work. You said you are working online, can you mention what framework or tool you are using? Maybe can find an API for your requirement.
Im working on client's environment, where i have access to using Python alone and actual problem is to webscrape content directly to PDF using selenium (or any other option if available)

I don't see a way to do this without using external libraries. @SAK

Directly creating a file and writing in it using open method also won't work. You have to at least run a pip install command.

K sure. Whats the way to make it run using any package installed via pip???

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.

0 votes

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

Hope this helps!!

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


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,080 points
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,870 points
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
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 2,915 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
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
Send OTP
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP