How to split a pdf into pages in Python

 · 1 min read

Hi friends, let’s say in a folder you have a pdf file which has 6 pages, and now you want to cut that pdf file because you are interested only in the 2nd page. So, for this all we need is to install pypdf using pip3 install pypdf2, and then in the same folder run the python script below:

from PyPDF2 import PdfFileWriter, PdfFileReader
import argparse
parser = argparse.ArgumentParser()

                    help="input pdf file location",

args = parser.parse_args()

inputpdf = PdfFileReader(open(args.pdf, "rb"))
for i in range(inputpdf.numPages):
    output = PdfFileWriter()
    with open("document-page%s.pdf" % i, "wb") as outputStream:

print(f"PDF named {args.pdf} splitted into {i+1} pages")

In the terminal, we need to pass -pdf argument to get the location of the pdf file, in this case our a.pdf file is in the same folder as


python3.6 -pdf a.pdf


PDF named a.pdf splitted into 6 pages