Python 3 PikePDF Library to Encrypt & Lock PDF Document With Password & Save it as PDF in Terminal
4K views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-pikepdf-library-to-encrypt-lock-pdf-document-with-password-save-it-as-pdf-in-terminal/
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
So in this video I will show you a
0:04
Python package which allows you to uh
0:07
encrypt or lock your PDF documents with
0:11
a uh custom password So there will be
0:14
many occasions whenever you want to
0:16
share PDF files with your friends and
0:19
then lock those PDF files with a
0:22
password So in those scenarios
0:25
uh this application will be really
0:27
useful So this is actually the script
0:29
here Uh we all we are taking a simple
0:32
example here sample PDF file which is
0:35
present right in the same directory I
0:37
will be locking this PDF file with a
0:39
custom password So as you can see we are
0:42
putting the password here secure 1 2 3
0:45
So if I execute this Python script it
0:47
will create another PDF file which will
0:50
be encrypted So as soon as I execute
0:52
this Python script you will see
0:54
encrypted PDF file has been successfully
0:56
saved
0:57
And if I try to open this it will ask me
1:01
for the password So here it is saying to
1:03
us that enter the password to open the
1:05
PDF file So if I enter incorrect
1:08
password you will see invalid password
1:12
So I just need to now enter the correct
1:14
password and enter Now the PDF file
1:18
opens So
1:21
uh the package that we are using is Pike
1:24
PDF Uh it's a very popular package Uh
1:27
you just need to install this So just uh
1:30
search for this package
1:33
pie pdf This is actually the package
1:37
here And uh simply install this by
1:41
executing this command Uh I've already
1:44
installed it So after installing it all
1:48
the source code is given in the
1:49
description of the video So now let me
1:51
show
1:53
you how to use this library So first of
1:56
all you need to import this package by
1:59
using the import line import peak
2:02
PDF After that we simply define a simple
2:06
function which will
2:08
encrypt the PDF file
2:12
So here we will simply pass the PDF file
2:15
which is present in the same directory
2:17
and then the output file name which will
2:20
be encrypted PDF and then we just need
2:24
to provide the password here Let me say
2:26
1 2 3 4 5 6 So now I just define this
2:30
function which is
2:34
encrypt PDF file and it actually takes
2:38
these three arguments First of all your
2:43
input PDF path Then we have the output
2:47
PDF path and then we have the password
2:51
So inside this function we actually
2:54
write the code inside the try catch
2:56
block and right inside this we
2:59
initialize the library peak PDF and it
3:03
contains this function of uh open First
3:06
of all we open this input
3:09
PDF
3:11
file After opening it we save this by
3:15
using the save function Add that
3:18
specified file And then here it takes an
3:21
argument here which is encryption So
3:23
here you provide your encryption your
3:27
password encryption And here we specify
3:31
the password that we are specifying So
3:37
password and then the user is equal
3:42
to password
3:45
password and then the third argument is
3:48
the R value R is equal to 4 that's all
3:52
So after
3:54
that we close the PDF
3:58
connection and then we simply print out
4:01
that your encrypted file has
4:04
been
4:07
saved successfully That's all And after
4:11
that if any sort of error take place
4:14
then we can
4:16
simply print out that error message
4:22
So this is a very simple application We
4:25
simply require the package Then we
4:28
declare this passing this input file
4:31
name Then we create this output file
4:33
name Then we provide the password and
4:35
write inside this function We use this
4:37
open function first of all to open the
4:39
PDF file Then we call the save function
4:42
to save the PDF file with those pasted
4:46
encryption the password that you pass So
4:49
if I try to execute this now uh you will
4:52
see on the left hand side the encryption
4:55
will be complete and it will create this
4:57
encrypted file If I try to open this
5:00
file you will see it will ask for the
5:02
password So you just need to provide the
5:05
correct password and then the PDF file
5:08
will open So in this easy way you can
5:10
use this Python
5:12
package to lock your PDF file with a
5:16
custom password and then you can share
5:18
it with your friends So thank you very
5:21
much for watching this video and also
5:23
check out my website
5:24
freemediattoolsh.com
5:27
uh which contains uh thousands of tools
#Scripting Languages
#Software