1

So I'm using the tkinter and pymupdf libraries to add blank pages to a desired location. This is done by pressing a button which inserts the blank page below the page on the button. My issue is that once it inserts the blank page, the original page order now changes.

When you press a button corresponding to a page number, it inserts the blank page, but if you try with a second blank page, it inserts it at the wrong place because it messes up the button's assigned page number. Please help I'm losing my hair.

import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import asksaveasfilename
import fitz

btnClick = 0


def launchInsert():
        global root
        global buttonframe
        root = tk.Tk()
        root.title("Bla bla bla")
        root.geometry("800x600")

        #Displays "text" name as title of program
        label = tk.Label(root, text ="Insert Blank Page", font=('Arial',18))
        label.pack(padx=20, pady=20)

        #File Explorer Button
        buttonframe = tk.Frame(root, padx=100, pady=25)
        btn1 = tk.Button(buttonframe,text="Browse...", font=('Arial',18), command=browse_file)
        btn1.grid(row=1, column=6, padx=225, sticky=tk.W+tk.E)
        buttonframe.pack(side="bottom")

def save_file():
    
    f = asksaveasfilename(initialfile = 'untitled.pdf',
    defaultextension=".pdf",filetype=[("All Files","*.*"),("PDF    Files","*.pdf")])
    pdf2.save(f)
    
    

    # with open(file, "wb") as pdfCombined:
    #     newPdf.write(pdfCombined)

def blank(i):
    
    #program is recognizing blank page as document original page then counting it as pagenumber
        #try to just ignore blank pages
    pdf2.new_page(pno=i, width=595, height=842)
    
    
    global btnClick
    btnClick = btnClick + 1
    if btnClick == 1:
        pdf2.new_page(pno=i, width=595, height=842)

    elif btnClick > 1:
        print("This is button click: ", btnClick)
        i = i + 1
        pdf2.new_page(pno=i, width=595, height=842)
    for slices in range(len(pdf2)):
        print("This is new page - ", slices)
    print("This is pageCount after addition", slices)
    print("The page was added successfully")
    print("This is ", i)

def browse_file():
        global filename
        filename = filedialog.askopenfilename(title="Select a File", filetype=(("PDF    Files","*.pdf"),("All Files","*.*")))

        global pdf2, pageCount, i
        pdf2 = fitz.open(filename)
        
        

        # Create A Main Frame
        main_frame = Frame(root)
        main_frame.pack(fill=BOTH, expand=1)

        # Create A Canvas
        my_canvas = Canvas(main_frame)
        my_canvas.pack(side=LEFT, fill=BOTH, expand=1)

        # Add A Scrollbar To The Canvas
        my_scrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview)
        my_scrollbar.pack(side=RIGHT, fill=Y)

        # Configure The Canvas
        my_canvas.configure(yscrollcommand=my_scrollbar.set)
        my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all")))

        # Create ANOTHER Frame INSIDE the Canvas
        second_frame = Frame(my_canvas)

        # Add that New frame To a Window In The Canvas
        my_canvas.create_window((0,0), window=second_frame, anchor="nw")

        btn2 = tk.Button(buttonframe,text="Save", font=('Arial',18), command=save_file)
        btn2.grid(row=1, column=7, pady=20, sticky=tk.W+tk.E)
        buttonframe.pack(side="bottom")
        
        for i in range(len(pdf2)):
            
            insClick = tk.Button(second_frame, text=f'Insert\nbelow {i}', padx= 50, command= lambda i=i: blank(i)).grid(row=i, column=5, pady=25, padx=50)

            numShow = Label(second_frame, text=f"Page number {i}",padx=200).grid(row=i, column=1)

launchInsert()
mainloop()

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 4
    I'm not sure I really understand, but inserting new pages **_inevitably_** renumbers all later pages. The only solution I see is **not** immediately adding blank pages, but postpone this until you really save. Instead, keep a list of page numbers after which you want to add empty pages. Once you finally do this, make sure to do it back to front - by descending page number. – Jorj McKie Jan 23 '23 at 22:00
  • Thank you for your response. I’ve taken it into consideration, however, the fitz.open() function doesn’t seem to allow for editing the file without saving the file as changes are made. My goal would be to have the user save all changes at the end. Is there a better way to go about doing that? – mrawesome0238 Jan 25 '23 at 17:59
  • Can't you just do as Jorj McKie suggests and save the file each time ? – Jack Griffin Mar 05 '23 at 14:49

0 Answers0