|
| 1 | +from PyPDF2 import PdfFileWriter, PdfFileReader |
| 2 | +from tkinter import * |
| 3 | +from tkinter.filedialog import * |
| 4 | +import PyPDF2 as pypdf |
| 5 | +import sys, os |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +def resource_path(relative_path): |
| 10 | + if hasattr(sys, '_MEIPASS'): |
| 11 | + return os.path.join(sys._MEIPASS, relative_path) |
| 12 | + return os.path.join(os.path.abspath("."), relative_path) |
| 13 | + |
| 14 | + |
| 15 | +root=Tk() |
| 16 | +root.title('Answers Hider') |
| 17 | +#root.iconbitmap( resource_path('./icon.ico')) |
| 18 | +pdf_list = [] |
| 19 | + |
| 20 | + |
| 21 | +filename1=StringVar() |
| 22 | +src_pdf=StringVar() |
| 23 | + |
| 24 | +def load_pdf(filename): |
| 25 | + f = open(filename,'rb') |
| 26 | + return pypdf.PdfFileReader(f) |
| 27 | + |
| 28 | +def load1(): |
| 29 | + f = askopenfilename(filetypes=(('PDF File', '*.pdf'), ('All Files','*.*'))) |
| 30 | + filename1.set(f.split('/')[-1]) |
| 31 | + src_pdf=f |
| 32 | + print(f) |
| 33 | + print(src_pdf) |
| 34 | + pdf1 = load_pdf(f) |
| 35 | + pdf_list.append(pdf1) |
| 36 | + pdf_list.append(f) |
| 37 | + |
| 38 | +def add_to_writer(pdfsrc, writer): |
| 39 | + [writer.addPage(pdfsrc.getPage(i)) for i in range(pdfsrc.getNumPages())] |
| 40 | + writer.removeImages() |
| 41 | + |
| 42 | +def remove_images(): |
| 43 | + print("remove rectangles and images") |
| 44 | + writer = PdfFileWriter() |
| 45 | + |
| 46 | + output_filename= asksaveasfilename(filetypes=(('PDF File', '*.pdf'), ('All Files','*.*'))) |
| 47 | + outputfile= open(output_filename+".pdf",'wb') |
| 48 | + |
| 49 | + add_to_writer(pdf_list[0], writer) |
| 50 | + |
| 51 | + #pdf_src = PdfFileReader(inputStream) |
| 52 | + |
| 53 | + writer.write(outputfile) |
| 54 | + outputfile.close() |
| 55 | + root.quit() |
| 56 | + |
| 57 | +##Label(root, text="Rectangles remover").grid(row=0, column=2, sticky=E) |
| 58 | +Button(root, text="Choose file", command=load1).grid(row=1, column=0) |
| 59 | +Label(root, textvariable=filename1,width=20).grid(row=1, column=1, sticky=(N,S,E,W)) |
| 60 | +#photo= PhotoImage(file=resource_path('./button_pic.png')) |
| 61 | + |
| 62 | +#Button(root, text="Remove answers",image=photo, command=remove_images, width=100, height=120).grid(row=1, column=2,sticky=E) |
| 63 | +Button(root, text="Remove answers", command=remove_images).grid(row=1, column=2,sticky=E) |
| 64 | + |
| 65 | +#Label(root, text="Remove Answers^^").grid(row=2, column=2, sticky=E) |
| 66 | +Label(root, text="Good Luck!").grid(row=2, column=0, sticky=W) |
| 67 | + |
| 68 | +for child in root.winfo_children(): |
| 69 | + child.grid_configure(padx=10,pady=10) |
| 70 | + |
| 71 | +root.mainloop() |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
0 commit comments