# How to split a pdf into pages in Python

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:

### cut.py

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

required=True,
help="input pdf file location",
)

args = parser.parse_args()

for i in range(inputpdf.numPages):
output = PdfFileWriter()
with open("document-page%s.pdf" % i, "wb") as outputStream:
output.write(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 cut.py.

### Usage:

python3.6 cut.py -pdf a.pdf


Output:

PDF named a.pdf splitted into 6 pages