Python 3 Matplotlib Script to Export Source Code & Text With Syntax Highlighting to PDF Document
300 views
Jun 1, 2025
Get 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 Python
0:04
script which will automatically convert
0:07
the text into PDF document it can
0:09
contain source code as well it can
0:12
syntax highlight the source code in any
0:14
programming language it can be in PHP
0:17
Python JavaScript HTML CSS so I've just
0:20
written this simple Python script it
0:22
uses this library called as math plot
0:25
lip if you search for this package here
0:28
which is a very simple package here very
0:30
popular package as well which is
0:32
mattplot
0:34
lip so it's a python package plotting
0:37
package the command is simple you simply
0:39
execute this command pip install
0:41
mattplot
0:43
lip so I've already installed it so I've
0:47
given the script in the description of
0:49
the video so now let me just show you
0:53
how it looks like first of all I will
0:57
execute this
0:59
script so as I execute this you will see
1:03
it will create a new
1:06
file sorry file name is slightly
1:09
different which is app 34
1:12
py so now it you will see the PDF is
1:16
successfully saved
1:18
successfully it gives the file name
1:21
as output pdf so if I try
1:24
to open this this is the file
1:27
here you will see it actually converted
1:30
all this Python script that I written
1:32
into a PDF file so it syntax
1:34
highlighting is preserved you will see
1:36
the formatting every part of this Python
1:38
script is preserved so in this easy way
1:41
you can actually convert source code of
1:44
any language and convert this to a PDF
1:46
document
1:48
so so now let me show you how this build
1:52
how you can easily do this so first of
1:55
all you need to import the package by
1:57
using import mattplot lip it contains
2:00
this function py plot as plt after that
2:04
here you need to specify the input file
2:06
name which is uh whatever file name that
2:09
you want to give so you give the path of
2:12
the file after that you specify the
2:15
output file result PDF and then we
2:19
simply open this file input file in the
2:24
read
2:27
mode like this after that whatever text
2:31
is read so we use the read function like
2:35
this and then we say figure ax and we
2:39
use this plt uh function right here
2:42
which is
2:43
subplot
2:46
subplot subplots function after that we
2:50
plot this text right in the pdf file
2:53
here by using the text
2:56
method and here we specify the
2:58
coordinates x coordinate and then we
3:01
specify the y-coordinate and the actual
3:03
text and wrap is equal to
3:07
true and then we also specify the phone
3:10
size so here you can even control the
3:12
phone size as well so let me say 18 you
3:15
can pass any value after that we simply
3:18
say access and we simply draw this
3:22
text to off that's all and then we save
3:27
this as a simple figure by using this uh
3:31
save figure
3:34
function and we simply give the format
3:37
here which is in PDF that's
3:40
all so that's all that we need to do
3:43
after that it will create this if you
3:46
just need to run
3:51
this you just need to make sure uh phone
3:54
size S is
3:58
small just run this it
4:03
will it is saying that uh
4:11
out sorry plot
4:21
save plot
4:29
X it is set to output sorry so here we
4:33
just need to replace
4:36
output so just run this it will create
4:39
this file here which is result PDF if
4:42
you see the file has been successfully
4:45
created you will see all your Python
4:47
code is successfully converted to a PDF
4:50
file so in this way you can do this for
4:52
any programming language using this
4:54
source code also check out my website
4:57
freemediattools.com
4:59
uh which contains uh thousands of tools
#Programming
#Scripting Languages
#Software