Skip to content

Commit

Permalink
Update and rename main.py to pdf_utility.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekthedev authored Jan 26, 2022
1 parent aa37806 commit dc152fe
Showing 1 changed file with 66 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import os
from PyPDF2 import PdfFileReader, PdfFileWriter

## Helper Functions

### Get PDF details in the window
def extract_information(pdf_path):
new_window = tk.Toplevel(root)
new_window.title(pdf_path)
Expand All @@ -27,57 +29,80 @@ def extract_information(pdf_path):
new_label = tk.Label(new_window, text=txt)
new_label.place(x=10, y=10)


# Read a pdf then create a new pdf with each page rotated
def rotate_pages(pdf_path):

# Output file object
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(pdf_path)

# Copy each page to the output file object
for i in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(i).rotateClockwise(90))

# Export the object in a pdf file
with open("./rotate_pages.pdf", "wb") as fh:
pdf_writer.write(fh)


# Read two pdfs and merge them into one
def merge_pdfs(pdfA, pdfB):

# Reading file objects of both pdfs
pdfA_obj = PdfFileReader(pdfA, strict=False)
pdfB_obj = PdfFileReader(pdfB, strict=False)

# Object to store merged pdf
pdf_merge = PdfFileWriter()

# Get all the pages from pdfA to writer object
for pgNum in range(pdfA_obj.numPages):
pg = pdfA_obj.getPage(pgNum)
pdf_merge.addPage(pg)

# Get all the pages from pdfB to writer object
for pgNum in range(pdfB_obj.numPages):
pg = pdfB_obj.getPage(pgNum)
pdf_merge.addPage(pg)

# Write out the merged PDF

fileObj = open(".\merged_pdf.pdf", "wb")
pdf_merge.write(fileObj)

#Display confirmation message
info_label.configure(text=f"File Saved at : {os.getcwd()}\merged_pdf.pdf")


# Split one pdf in two different pdfs
def pdf_split(path, ranges):

# Read original pdf
pdf = PdfFileReader(path, strict=False)

# Writer obeject for pdf
pdf_writer = PdfFileWriter()

# Get all the pages upto the provided page number
for page in range(int(ranges)):
pdf_writer.addPage(pdf.getPage(page))

# Export the object to pdf file for the first part of the splitted pdf
with open("./SplitA.pdf", "wb") as output_pdf:
pdf_writer.write(output_pdf)

# Same steps for the second part of the pdf.
pdf_writer = PdfFileWriter()
for page in range(int(ranges), pdf.getNumPages()):
pdf_writer.addPage(pdf.getPage(page))
with open("./SplitB.pdf", "wb") as output_pdf:
pdf_writer.write(output_pdf)


# Overlay water mark on a pdf (watermark should also be a pdf)
def create_watermark(input_pdf, watermark):

# Get watermark page after reading the pdf
watermark_obj = PdfFileReader(watermark, strict=False)
watermark_page = watermark_obj.getPage(0)

pdf_reader = PdfFileReader(input_pdf, strict=False)
pdf_writer = PdfFileWriter()

Expand All @@ -86,46 +111,59 @@ def create_watermark(input_pdf, watermark):
page = pdf_reader.getPage(page)
page.mergePage(watermark_page)
pdf_writer.addPage(page)


# Export the writer object to a pdf
with open("./Watermarked.pdf", "wb") as out:
pdf_writer.write(out)
info_label.configure(text=f"Watermark set\nCheck : {os.getcwd()}")


# Make a pdf password protected
def add_encryption(input_pdf, password):

# Reader and writer objects
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(input_pdf, strict=False)

# Copy all the pages from the pdf to the writer object
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))


# add encryption to a writer object
pdf_writer.encrypt(user_pwd=password, owner_pwd=None, use_128bit=True)

# export the encrypted pdf
f = open("./Encrypted.pdf", "wb")
pdf_writer.write(f)
info_label.configure(text=f"Password Set\nCheck : {os.getcwd()}")


## Selection Functions

# Select file and call extract_information()
def info():
path = askopenfilename(title="Select File", filetypes=[("PDF Document", "*pdf")])
extract_information(path)
if path:
extract_information(path)


# Select file and call rotate_pages()
def rotate():
path = askopenfilename(title="Select File", filetypes=[("PDF Document", "*pdf")])
rotate_pages(path)

if path:
rotate_pages(path)

# Select two files and call merge_pdfs()
def merge():
path_A = askopenfilename(
title="Select First File", filetypes=[("PDF Document", "*pdf")]
)
path_B = askopenfilename(
title="Select Second File", filetypes=[("PDF Document", "*pdf")]
)
if all(path_A, path_B):
merge_pdfs(path_A, path_B)

merge_pdfs(path_A, path_B)


#Select file and get range and then call pdf_split
def split():
path = askopenfilename(title="Select File", filetypes=[("PDF Document", "*pdf")])

Expand All @@ -135,6 +173,8 @@ def assign():
newWindow.destroy()

if path:
# Small windo basic tkinter setup
# this window is responsible of creating a popup and ask user the required informationn
newWindow = tk.Toplevel(root)
newWindow.title("Enter Split Ranges")
newWindow.geometry("400x200")
Expand All @@ -154,6 +194,7 @@ def assign():
container.grid(row=2, column=0)


# Select two files and call creat_watermark()
def watermark():
path = askopenfilename(title="Select File", filetypes=[("PDF Document", "*pdf")])
pathW = askopenfilename(
Expand All @@ -172,6 +213,9 @@ def assign():
newWindow.destroy()

if path:

# Small windo basic tkinter setup
# this window is responsible of creating a popup and ask user the required informationn
newWindow = tk.Toplevel(root)
newWindow.title("Create Password")
newWindow.geometry("400x200")
Expand All @@ -191,32 +235,36 @@ def assign():
container.grid(row=1, column=0)


# Basic root tkinter setup, geometry, title
root = tk.Tk()
root.geometry("800x600")
root.wm_title("PDF Utility Tool")
root.resizable(False, False)
fontStyle = tkFont.Font(size=42)
btnText = tkFont.Font(size=12)


# Creating two rows and one column in the window
root.columnconfigure(0, minsize=800)
root.rowconfigure(0, minsize=200)
root.rowconfigure(2, minsize=400)

# Title label
label1 = tk.Label(
text="PDF Utility Tool", font=fontStyle, borderwidth=2, relief="groove"
)
info_label = tk.Label(text="", font=btnText)


# Information label shows the confirmation messages
info_label = tk.Label(text="", font=btnText)
label1.grid(row=0, column=0)
info_label.place(x=200, y=150)

# Container to store all the function buttons
container = tk.Frame(root)

container.columnconfigure([0, 1], minsize=300)
container.rowconfigure([0, 1, 2], minsize=100)

# Button for evry function and custom command attribute to call the functions
btn_merge = tk.Button(
container,
text="Merge",
Expand Down Expand Up @@ -279,6 +327,7 @@ def assign():
)


# Placing all the buttons in the grid setup
btn_merge.grid(row=0, column=0)
btn_rotate.grid(row=0, column=1)
btn_split.grid(row=1, column=0)
Expand Down

0 comments on commit dc152fe

Please sign in to comment.