Split every page in a pdf i a different pdf

This code will get as output as many pdf of 1 page as many pages are in the input pdf

from PyPDF2 import PdfFileWriter, PdfFileReader


# change the filetosplit.pdf name with the one you wanto to split
inputpdf = PdfFileReader("filetosplit.pdf", "rb")
print(inputpdf)
for i in range(inputpdf.numPages):
    output = PdfFileWriter()
    output.addPage(inputpdf.getPage(i))
    with open("document-page%s.pdf" % i, "wb") as outputStream:
        output.write(outputStream)

A graphic user interface

In this code we add a GUI to select the pdf we want to split

import tkinter as tk
from PyPDF2 import PdfFileWriter, PdfFileReader
import glob


def split(file):
    inputpdf = PdfFileReader(file, "rb")
    print(inputpdf)
    for i in range(inputpdf.numPages):
        output = PdfFileWriter()
        output.addPage(inputpdf.getPage(i))
        with open("document-page%s.pdf" % i, "wb") as outputStream:
            output.write(outputStream)
    lb.delete(0, tk.END)
    print(glob.glob("*.pdf"))
    for file in glob.glob("*.pdf"):
        lb.insert(tk.END, file)


root = tk.Tk()
root.geometry("400x400")
lb = tk.Listbox(root)
lb.pack()
for file in glob.glob("*.pdf"):
    lb.insert(tk.END, file)
lb.bind("<Double-Button>", lambda x: split(lb.get(lb.curselection()[0])))

root.mainloop()

The video

Adding more features

Now you get more features. You have the chance to delete the splitted pages and to open them from the gui directly.

import tkinter as tk
import tkinter.ttk as ttk
from PyPDF2 import PdfFileWriter, PdfFileReader
import glob
import os
from tkinter import messagebox


def split(file):
    inputpdf = PdfFileReader(file, "rb")
    print(inputpdf)
    for i in range(inputpdf.numPages):
        output = PdfFileWriter()
        output.addPage(inputpdf.getPage(i))
        with open("output/document-page%s.pdf" % i, "wb") as outputStream:
            output.write(outputStream)
    lb.delete(0, tk.END)
    print(glob.glob("*.pdf"))
    refreshlist(lb2)


def refreshlist(lst):
    if lst == lb:
        folder = "input"
    elif lst == lb2:
        folder = "output"
        lst.delete(0, tk.END)
    for file in glob.glob(folder + "/*.pdf"):
        lst.insert(tk.END, file)

root = tk.Tk()
root.geometry("400x400")
root["bg"] = "green"
lb = tk.Listbox(root)
lb.pack(side="left", fill=tk.BOTH, expand=1)
refreshlist(lb)
lb.bind("<Double-Button>", lambda x: split(lb.get(lb.curselection()[0])))

lb2 = tk.Listbox(root)
lb2.pack(side="left", fill=tk.BOTH, expand=1)
refreshlist(lb2)
lb2.bind("<Double-Button>", lambda x: os.startfile(lb2.get(lb2.curselection())))

def remove():
    for f in glob.glob("output/*.pdf"):
        os.remove(f)
    refreshlist(lb2)

b2 = ttk.Button(root, text="remove output", command=remove)
b2.pack()

def split2():
    print(lb.curselection())
    if lb.curselection() != ():
        split(lb.get(lb.curselection()))
    else:
        messagebox.showwarning("Select something first")


b3 = ttk.Button(root, text="Split file", command=split2)
b3.pack(fill="both")

root.mainloop()


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.