Split PDF into single Files using Python

Split PDF into single files using Python code,split any given PDF into single files at the destination folder. This program is required my all of us to split PDF files into multiple files, this is one such task that all of us do. This simple Python program is written using PyPDF2 library that you can install very easily using pip command. Here is the pip command to install the same

pip install pypdf2

This library has two classes for reading PDF and for writing PDF files.  We are already aware that PDF files are also treated as binary files so we will open PDF in binary mode and write single page as a stand-alone PDF file in our destination folder.

How to run PDF splitter Python Program

  1. First of all, make it very sure that Python is installed on your system.
  2. If python is there then copy and paste the following code inside your IDE and run this program.
  3. Now select the PDF file that you want to split.
  4. Now select the destination folder. This folder will have all your split PDF files.

Python code for spiliting PDF file into multiple files.

#-------------------------------------------------------------------------------
# Name:        pdf spiller
# Purpose:     split pdf in to single pages
# Author:      acer
## Created:     13-04-2018
# Copyright:   (c) acer 2018
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import os
import sys          # module for terminating 
import glob         # 
import tkinter as tk
from tkinter import filedialog
from PyPDF2 import PdfFileReader, PdfFileWriter
def pdf_splitter(path,dest):
    fname = os.path.splitext(os.path.basename(path))[0]
    pdf = PdfFileReader(path)
    for page in range(pdf.getNumPages()):
        pdf_writer = PdfFileWriter()
        pdf_writer.addPage(pdf.getPage(page))
        output_filename = os.path.join(dest,'{}_page_{}.pdf'.format(fname, page+1))
        #print(output_filename)
        with open(output_filename, 'wb') as out:
            pdf_writer.write(out)
        print('Created: {}'.format(output_filename))

if __name__ == '__main__':
    #path = "Computer_Science.pdf"
    root = tk.Tk()
    root.withdraw()
    path = filedialog.askopenfilename()  #source file name
    dest = filedialog.askdirectory()     # destination folder
    pdf_splitter(path,dest)

You can extract the exact page number of your selected PDF file by changing the page number in this for loop.

for page in range(pdf.getNumPages()):

In this case, if you want to extract page no, 1,15,20 then all you need is to define these page number as a tuple and pass it herein this for loop. Now your for loop will look like this

pages =[2,5,10]
for pages in pages:

rest will remain same so your final program will look like this.

#-------------------------------------------------------------------------------
# Name:        pdf spiller
# Purpose:     split pdf in to single pages
# Author:      acer
## Created:     13-04-2018
# Copyright:   (c) acer 2018
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import os
import sys          # module for terminating 
import glob         # 
import tkinter as tk
from tkinter import filedialog
from PyPDF2 import PdfFileReader, PdfFileWriter
def pdf_splitter(path,dest):
    fname = os.path.splitext(os.path.basename(path))[0]
    pdf = PdfFileReader(path)
    pages = [2,5,10]
    for page in pages:
        pdf_writer = PdfFileWriter()
        pdf_writer.addPage(pdf.getPage(page))
        output_filename = os.path.join(dest,'{}_page_{}.pdf'.format(fname, page+1))
        #print(output_filename)
        with open(output_filename, 'wb') as out:
            pdf_writer.write(out)
        print('Created: {}'.format(output_filename))

if __name__ == '__main__':
    #path = "Computer_Science.pdf"
    root = tk.Tk()
    root.withdraw()
    path = filedialog.askopenfilename()  #source file name
    dest = filedialog.askdirectory()     # destination folder
    pdf_splitter(path,dest)

Now run this modified program and enjoy. Hope this Python program to split PDF file is helpful. If you have any query, then please submit via your comments.

Leave a Reply

Your email address will not be published. Required fields are marked *