diff --git a/scripts/PDF-delete-pages/README.md b/scripts/PDF-delete-pages/README.md new file mode 100644 index 0000000..6d5a4cc --- /dev/null +++ b/scripts/PDF-delete-pages/README.md @@ -0,0 +1,19 @@ +# PDF Delete Pages + +A simple Tkinter GUI application that deletes specified pages from a PDF and outputs a new PDF file with only the desired pages. + +### Installing Requirements + - Open Windows Command Prompt + - Run command ``` pip install PyPDF2 ``` + +### Usage + - Install the requirements (Python3 and PyPDF2 module) + - Run the application script + - Select the PDF file + - Enter page numbers to be deleted + - Wait for confirmation + - New PDF file will be generated at the location of the script. + +### Screenshots + ![Screenshot1](/scripts/PDF-delete-pages/example/screenshot1.jpg?raw=true) + ![Screenshot2](/scripts/PDF-delete-pages/example/screenshot2.jpg?raw=true) diff --git a/scripts/PDF-delete-pages/example/Test.pdf b/scripts/PDF-delete-pages/example/Test.pdf new file mode 100644 index 0000000..b187f91 Binary files /dev/null and b/scripts/PDF-delete-pages/example/Test.pdf differ diff --git a/scripts/PDF-delete-pages/example/Test_deleted.pdf b/scripts/PDF-delete-pages/example/Test_deleted.pdf new file mode 100644 index 0000000..9bb41a5 Binary files /dev/null and b/scripts/PDF-delete-pages/example/Test_deleted.pdf differ diff --git a/scripts/PDF-delete-pages/example/screenshot1.jpg b/scripts/PDF-delete-pages/example/screenshot1.jpg new file mode 100644 index 0000000..19c80c1 Binary files /dev/null and b/scripts/PDF-delete-pages/example/screenshot1.jpg differ diff --git a/scripts/PDF-delete-pages/example/screenshot2.jpg b/scripts/PDF-delete-pages/example/screenshot2.jpg new file mode 100644 index 0000000..61b869d Binary files /dev/null and b/scripts/PDF-delete-pages/example/screenshot2.jpg differ diff --git a/scripts/PDF-delete-pages/script.py b/scripts/PDF-delete-pages/script.py new file mode 100644 index 0000000..d5b61c9 --- /dev/null +++ b/scripts/PDF-delete-pages/script.py @@ -0,0 +1,72 @@ +from PyPDF2 import PdfFileWriter, PdfFileReader +import os +import tkinter as tk +from tkinter import ttk +from tkinter.filedialog import askopenfilename + + +win = tk.Tk() +win.geometry("400x200") +win.title("Delete PDF Pages") + + +def pagedel(): + pages_del = pages.get() + pages_to_delete = pages_del.strip().split(",") + pages_to_delete = [(int(i) - 1) for i in pages_to_delete] + + with open(path, "rb") as pdf_file: + pdf_reader = PdfFileReader(pdf_file) + num_pages = pdf_reader.numPages + + out_of_index_page = [] + for num in pages_to_delete: + if num > num_pages: + out_of_index_page.append(num) + + if len(out_of_index_page) == 0: + + infile = PdfFileReader(path, "rb") + output = PdfFileWriter() + + for i in range(infile.getNumPages()): + if i not in pages_to_delete: + p = infile.getPage(i) + output.addPage(p) + + inputfile_name = ((path.split("\\")[-1]).split(".pdf"))[0] + + output_name = inputfile_name + "_deleted.pdf" + + with open(output_name, "wb") as f: + output.write(f) + + print(f"\nThe output pdf is saved as: {output_name}\n") + + else: + print("\nPage number entered is greater than the No of Pages in PDF") + print("Please Check & Re-Try\n") + + +def getpages(): + global pages, path + user_path = askopenfilename().strip() + path = os.path.normpath(user_path) + + label2 = ttk.Label(win, text="Enter page numbers to be deleted seperated by commas:\n(Eg: 1,2) ") + label2.grid(row=0, column=0, padx=50, pady=20) + + pages = tk.Entry(win, width = 30) + pages.grid(row=1, column=0, padx=50, pady=0) + + button2 = ttk.Button(win, text="Enter", width=30, command=pagedel) + button2.grid(row=2, column=0, padx=50, pady=10) + + +label1 = ttk.Label(win, text="Select PDF File: ") +label1.grid(row=0, column=0, padx=100, pady=40) + +button1 = ttk.Button(win, text="Select File", width=30, command=getpages) +button1.grid(row=1, column=0, padx=100, pady=0) + +win.mainloop()