0

I've been working on this particular problem for months now and have made no progress... The goal is to have a library of PDFs that you can add to a list, then click a button that will merge whatever PDFs you added and save said merged file to a directory of your choosing. I'm not good at explaining it so here is the whole code:

#Imports
import os
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askdirectory
from tkinter.filedialog import asksaveasfile
from PyPDF2 import PdfFileMerger

global CURRENT_DIR
CURRENT_DIR=os.path.dirname(os.path.abspath(__file__))

class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title('Test')
        self.geometry('900x650')
        
#container
        container = tk.Frame(self)
        container.pack(side = "top", fill = "both", expand = True)
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

#Class Frames
        self.frames = {}

    #Pages
        for F in (Home, Product_Data):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky ="nsew")

    #Startup Page
        self.show_frame(Home)

#Action
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

#Home Class
class Home(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) 
    
#Buttons
        button1 = tk.Button(self, text ="Product Data", compound='top', padx=30, pady= 60, relief='raised',
        command = lambda : controller.show_frame(Product_Data))
        button1.grid(row = 0, column = 0, padx = 30, pady = 30)

class Product_Data(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        #Frame Set Up
        mainframe = tk.Frame(self, height=400, width=950, relief= 'sunken')
        mainframe.pack(fill='both', expand=True)

        plu=tk.Frame(mainframe, height=70, width=950, relief= 'sunken')
        plu.pack(fill='both', side='top', expand=True)

        ntbk=tk.Frame(mainframe, height=550, width=950, relief= 'sunken')
        ntbk.pack(fill='both', side='top', expand=True)

        btm=tk.Frame(mainframe, height=550, width=950, relief= 'sunken')
        btm.pack(fill='both', side='top', expand=True)

        tab=tk.Frame(ntbk, height=100, width=950, relief= 'sunken')
        tab.pack(fill='both', side='top', expand=True)

    #Product Key Codes
        #TEST 1
        def Product_Look_UP():
            if inPro.get()== "T1I1":
                list.insert(0, "Test 1: Item 1")
                import webbrowser as wb
                wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 1\T1I1.pdf')
            elif inPro.get() == "T1I2":
                list.insert(0, "Test 1: Item 2")
                import webbrowser as wb
                wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 2\T1I2.pdf')
            elif inPro.get() == "T1I3":
                list.insert(0, "Test 1: Item 3")
                import webbrowser as wb
                wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 3\T1I3.pdf')     

        #TEST 2
            elif inPro.get()== "T2I1":
                list.insert(0, "Test 2: Item 1")
                import webbrowser as wb
                wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 2\ITEM 1\T2I1.pdf')
            elif inPro.get() == "T2I2":
                list.insert(0, "Test 2: Item 2")
                import webbrowser as wb
                wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 2\ITEM 2\T2I2.pdf')
            elif inPro.get() == "T2I3":
                list.insert(0, "Test 2: Item 3")
                import webbrowser as wb
                wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 2\ITEM 3\T2I3.pdf')     
    
        #TEST 3
            elif inPro.get()== "T3I1":
                list.insert(0, "Test 3: Item 1")
                import webbrowser as wb
                wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 1\T3I1.pdf')
            elif inPro.get() == "T3I2":
                list.insert(0, "Test 3: Item 2")
                import webbrowser as wb
                wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 3\ITEM 2\T3I2.pdf')
            elif inPro.get() == "T3I3":
                list.insert(0, "Test 3: Item 3")
                import webbrowser as wb
                wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 3\ITEM 3\T3I3.pdf')     

#Else Branch     
            else:
                f=tk.Label(plu, text= "Product not found..." , fg = "Orange")
                f.grid(row = 2 , column = 1)
                f.after(3000, lambda: f.destroy())

#Labels
        PK=tk.Label(plu, text= "Product Key:")
        PK.grid(row = 0 , column = 0)

        label=tk.Label(plu, text= "-OR-")
        label.grid(row = 2 , column = 1)

        #Entries
        inPro = tk.Entry(plu, width= 50 , borderwidth = 5)
        inPro.grid(row = 0, column = 1)

        #tk.Buttons
        B1=tk.Button(plu, text= "Search" , command =Product_Look_UP)
        B1.grid(row = 0, column = 2)

#Notebook
        notebook = ttk.Notebook(tab)
        notebook.pack(anchor='center', expand=True, fill='both')

        test_1 = ttk.Frame(notebook, width=400, height=280)
        test_2 = ttk.Frame(notebook, width=400, height=280)
        test_3 = ttk.Frame(notebook, width=400, height=280)

        test_1.pack(fill='both', expand=True)
        test_2.pack(fill='both', expand=True)
        test_3.pack(fill='both', expand=True)

        notebook.add(test_1, text='Test 1', compound="top")
        notebook.add(test_2, text='Test 2', compound="top")
        notebook.add(test_3, text='Test 3', compound="top")

        #Test 1 Notebook Layout
        tree1 = ttk.Treeview(test_1)
        tree1.pack(fill='both')

        tree1.insert(parent='', index='end', iid=0, text='Go to Test 1 Website')
        tree1.insert(parent='', index='end', iid=1, text='Test 1 Items')
        tree1.insert(parent='1', index='end', iid=2, text='Item 1')
        tree1.insert(parent='1', index='end', iid=3, text='Item 2')
        tree1.insert(parent='1', index='end', iid=4, text='Item 3')

        def on_double_click1(e):
                iid = tree1.focus()
                if iid == "0":
                        import webbrowser as wb
                        wb.open('https://www.test1.com')
                if iid == "2":
                        import webbrowser as wb
                        wb.open(f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 1\T1I1.pdf')
                if iid == "3":
                        import webbrowser as wb
                        wb.open(f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 2\T1I2.pdf')
                if iid == "4":
                        import webbrowser as wb
                        wb.open(f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 3\T1I3.pdf')

        tree1.bind('<Double-Button-1>', on_double_click1)

        #Test 2 Notebook Layout
        tree2 = ttk.Treeview(test_2)
        tree2.pack(fill='both')

        tree2.insert(parent='', index='end', iid=0, text='Go to Test 2 Website')
        tree2.insert(parent='', index='end', iid=1, text="Test 2 Items")
        tree2.insert(parent='1', index='end', iid=2, text='Item 1')
        tree2.insert(parent='1', index='end', iid=3, text='Item 2')
        tree2.insert(parent='1', index='end', iid=4, text='Item 3')

        def on_double_click2(e):
                iid = tree2.focus()
                if iid == "0":
                        import webbrowser as wb
                        wb.open('https://www.test2.com')
                if iid == "2":
                        import webbrowser as wb
                        wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 2\ITEM 1\T2I1.pdf')
                if iid == "3":
                        import webbrowser as wb
                        wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 2\ITEM 2\T2I2.pdf')
                if iid == "4":
                        import webbrowser as wb
                        wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 2\ITEM 3\T2I3.pdf')

        tree2.bind('<Double-Button-1>', on_double_click2)

        #Test 3 Notebook Layout
        tree3 = ttk.Treeview(test_3)
        tree3.pack(fill='both')

        tree3.insert(parent='', index='end', iid=0, text="Go to Test 3 Website")
        tree3.insert(parent='', index='end', iid=1, text="Test 3 Items")
        tree3.insert(parent='1', index='end', iid=2, text="Item 1")
        tree3.insert(parent='1', index='end', iid=3, text="Item 2")
        tree3.insert(parent='1', index='end', iid=4, text="Item 3")

        def on_double_click3(e):
                iid = tree3.focus()
                if iid == "0":
                        import webbrowser as wb
                        wb.open('https://www.test3.com')
                if iid == "2":
                        import webbrowser as wb
                        wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 3\ITEM 1\T3I1.pdf')       
                if iid == "3":
                        import webbrowser as wb
                        wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 3\ITEM 2\T3I2.pdf')
                if iid == "4":
                        import webbrowser as wb
                        wb.open_new(f'{CURRENT_DIR}\PDFS\TEST 3\ITEM 3\T3I3.pdf')

        tree3.bind('<Double-Button-1>', on_double_click3)

#Def
    #Adding PDF to Listbox
        #Test 1
        def add_test_1_pdf():
            iid=tree1.focus()
            
            if iid=='2':
                list.insert(0, "Test 1: Item 1")
            elif iid=='3':
                list.insert(1, "Test 1: Item 2")
            elif iid=='4':
                list.insert(2, "Test 1: Item 3")

            else:
                pass

        #Test 2
        def add_test_2_pdf():
            iid=tree2.focus()
            
            if iid=='2':
                list.insert(3, "Test 2: Item 1")
            elif iid=='3':
                list.insert(4, "Test 2: Item 2")
            elif iid=='4':
                list.insert(5, "Test 2: Item 3")

            else:
                pass
        #Test 3
        def add_test_3_pdf():
            iid=tree3.focus()
            
            if iid=='2':
                list.insert(6, "Test 3: Item 1")
            elif iid=='3':
                list.insert(7, "Test 3: Item 2")
            elif iid=='4':
                list.insert(8, "Test 3: Item 3")

            else:
                pass

        def remove_pdf():
            list.delete(ACTIVE)

        def delete_all():
            list.delete(0,END)

        def merge_pdf():

            #savedir = filedialog.askdirectory (title='Select folder')
            #os.makedirs(savedir)

            a=f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 1\T1I1.pdf'
            b=f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 2\T1I2.pdf'
            c=f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 3\T1I3.pdf'

            pdfs=[a,b,c]

            merger=PdfFileMerger()

            for pdf in pdfs:
                merger.append(pdf)
            merger.write('Combined.pdf')
            merger.close()
            
            file = asksaveasfile(title='Select Folder to Save Merged Product Data', initialdir=f'{CURRENT_DIR}\Merged File', initialfile = 'Combined.pdf',
                defaultextension=".pdf",filetypes=[("PDF","*.*"),("PDF Documents","*.pdf")])
            import webbrowser as wb
            wb.open(fr'{CURRENT_DIR}\Combined.pdf')

        #tk.Buttons
            #Home Button
        hb2=ttk.Button(plu, text="Back to Home",
        command = lambda : controller.show_frame(Home))
        hb2.grid(row=0, column=3)

            #Add Buttons
                #Test 1
        add_test_1=ttk.Button(test_1, text="ADD", command = add_test_1_pdf)
        add_test_1.pack(anchor=W)

                #Test 2
        add_test_2=ttk.Button(test_2, text="ADD", command = add_test_2_pdf)
        add_test_2.pack(anchor=W)

                #Test 3
        add_test_3=ttk.Button(test_3, text="ADD", command = add_test_3_pdf)
        add_test_3.pack(anchor=W)

            #Delete Selected Button
        remove=ttk.Button(tab, text="REMOVE", command = remove_pdf)
        remove.pack(anchor=W)

            #Delete All Button
        delete=ttk.Button(tab, text="Delete All", command = delete_all)
        delete.pack(anchor=W)

        #Merge Button
        merge=ttk.Button(tab, text="MERGE", command = merge_pdf)
        merge.pack(anchor=W)

        #List
        list=Listbox(btm, height=10, width=150, relief= 'sunken')
        list.grid(row=1, column=0)
        list.bind('<<Curselect>>')

app = App()
app.mainloop()

The problem I'm having is in the Merge def..

def merge_pdf():
   #savedir = filedialog.askdirectory (title='Select folder')
    #os.makedirs(savedir)

    a=f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 1\T1I1.pdf'
    b=f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 2\T1I2.pdf'
    c=f'{CURRENT_DIR}\PDFS\TEST 1\ITEM 3\T1I3.pdf'

    pdfs=[a,b,c]

    merger=PdfFileMerger()

    for pdf in pdfs:
        merger.append(pdf)
    merger.write('Combined.pdf')
    merger.close()

    file = asksaveasfile(title='Select Folder to Save Merged Product Data', initialdir=f'{CURRENT_DIR}\Merged File', initialfile = 'Combined.pdf',
        defaultextension=".pdf",filetypes=[("PDF","*.*"),("PDF Documents","*.pdf")])
    import webbrowser as wb
    wb.open(fr'{CURRENT_DIR}\Combined.pdf')

I can't figure out how to get whatever has been added to the list box and merge them, then choose the directory for said merged file to go. Obviously I could write a "If/Then" statement for every possible combination, but the main file has tens of thousands of individual PDFs. I hope I'm making sense. If I left any helpful information out, please let me know. Thanks in advance!

0 Answers0