Python 3 weasyprint Library Example to Convert HTML & CSS Template to PDF Document
66 views
Jun 3, 2025
Buy the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a package
0:04
in Python to actually convert your HTML
0:07
and CSS files into PDF so let me show
0:10
you the package name here so just search
0:13
for this package web print this is
0:16
actually the awesome document processing
0:19
library just install this package i've
0:22
already installed it pip
0:24
install
0:26
wint so once you install this two lines
0:29
of code is there so we simply import
0:32
this
0:33
package wint and from this we import
0:38
html after that we simply provide the
0:42
input file name that we need to convert
0:44
which is index html which is present in
0:47
my same directory and this contains a
0:49
function which is write pdf and right
0:53
here you simply provide the name of the
0:56
file let's suppose I say result dot PDF
1:00
so these are the two lines of code we
1:02
first of all import the module and then
1:05
it contains this write PDF method and we
1:09
convert this into PDF so now if I
1:12
execute this script here just wait so
1:14
this is our HTML file here if you see it
1:17
is present in the same directory and
1:19
this contains a series of headings
1:22
tables paragraphs as well and un
1:26
uh unordered list as well these elements
1:30
so if I execute the script you will see
1:34
a result PDF will be created and this
1:38
will actually contain your PDF file
1:40
right here so it successfully converted
1:42
your HTML into PDF and the nice thing is
1:46
that you can even actually import your
1:48
custom CSS so if I just make the custom
1:51
CSS paragraph to be color to be
1:56
green and then I make the H1 heading to
2:00
be color to be red so if you make the
2:03
change once again run the script you
2:06
will see now it will apply those CSS
2:09
styles to the
2:11
PDF so this is a very good package guys
2:14
inside Python to convert your HTML and
2:17
CSS to PDF
2:20
this is actually the package and also
2:22
check out my website
2:24
freemediatools.com which contains
2:26
thousands of tools
#Scripting Languages
#Software