Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Tkinter Script to Encrypt PDF Documents With Password Using PyPDF2 Library GUI Desktop App

Posted on March 12, 2023

 

Welcome folks today in this blog post we will be building a tkinter desktop app where we will be allowing the user to encrypt pdf documents with password using the pypdf2 library. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from tkinter import *
from tkinter import filedialog, simpledialog
from tkinter import ttk
import os
import PyPDF2
 
                        
# Create Tkinter window
root = Tk()
root.title("PDF Encryptor")
 
# Create main frame
main_frame = ttk.Frame(root, padding=20)
main_frame.grid(column=0, row=0, sticky="nsew")
 
# Create "Select PDF" label
select_pdf_label = ttk.Label(main_frame, text="Select PDF file to encrypt:")
select_pdf_label.grid(column=0, row=0, sticky="w")
 
# Create "Select PDF" button
select_pdf_button = ttk.Button(main_frame, text="Select PDF", command=encrypt_pdf)
select_pdf_button.grid(column=1, row=0, sticky="w")
 
 
# Configure main frame
main_frame.columnconfigure(0, weight=1)
main_frame.columnconfigure(1, weight=1)
main_frame.rowconfigure(0, weight=1)
main_frame.rowconfigure(1, weight=1)
main_frame.rowconfigure(2, weight=1)
 
root.mainloop()

 

 

As you can see we have a simple button where we allow the user to select the pdf file from the file system to encrypt and then we will be showing the messagebox to the user to enter the password to encrypt the pdf and then we will be allowing the user to enter the output filename where to save the encrypted pdf document and then we will be opening the pdf document automatically after it’s encrypted.

 

 

 

 

And now we need to write the function which will be encrypt_pdf() which will be executed once you click the button as shown below

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Function to encrypt PDF file
def encrypt_pdf():
    # Ask user to select input PDF file
    input_file_path = filedialog.askopenfilename(initialdir = "/", title = "Select file", filetypes = (("PDF files", "*.pdf"), ("All files", "*.*")))
    
    if input_file_path:
        # Ask user to enter password for encryption
        password = simpledialog.askstring("Password", "Enter password for encryption", show='*')
        
        if password:
            # Create encrypted PDF file
            output_file_path = filedialog.asksaveasfilename(initialdir = os.path.expanduser("~") + "/Documents", title = "Save encrypted file", initialfile = os.path.splitext(os.path.basename(input_file_path))[0] + "_encrypted.pdf", defaultextension=".pdf", filetypes = (("PDF files", "*.pdf"), ("All files", "*.*")))
            
            if output_file_path:
                with open(input_file_path, 'rb') as input_file:
                    pdf_reader = PyPDF2.PdfFileReader(input_file)
                    pdf_writer = PyPDF2.PdfFileWriter()
                    
                    for page_num in range(pdf_reader.numPages):
                        pdf_writer.addPage(pdf_reader.getPage(page_num))
                    
                    pdf_writer.encrypt(password)
                    
                    with open(output_file_path, 'wb') as output_file:
                        pdf_writer.write(output_file)
                        
                # Open encrypted PDF file
                os.startfile(output_file_path)

 

 

Recent Posts

  • Python 3 Tkinter Script to Encrypt PDF Documents With Password Using PyPDF2 Library GUI Desktop App
  • Node.js Express Project to Upload Image Buffer & BLOB Data to MongoDB Database & Display it in Browser Using Javascript
  • Node.js Tutorial to Export Images to PDF Document With Effects Using FilePix Library in Javascript
  • Node.js Tutorial to Export All Pages of PDF Document to Images and Save it in Javascript
  • Node.js OfficeGen Example to Add Text & Images in Powerpoint Presentation in Javascript
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme