Wednesday 6 March 2019

PDF Splitter using PyPDF2 module of Python - Split PDF into multiple pages

Often when working with a large PDF we fumble upon of a need where we need each page of the PDF in one separate  PDF file.

So in this article we are exactly going to do this but not using any Linux command but using Python.

For that lets get our dependencies get installed. Just run the below command and you are all ready.

pip3 install pypdf2

Now open a editor and save the following code in to your editor.

from PyPDF2 import PdfFileWriter, PdfFileReader

inputpdf = PdfFileReader(open("largefile.pdf", "rb"))

for i in range(inputpdf.numPages):
    output = PdfFileWriter()
    output.addPage(inputpdf.getPage(i))
    with open("largefile-page%s.pdf" % i, "wb") as outputStream:
        output.write(outputStream)

Thanks to StackOverflow for this part. Thats it now you are ready to split a PDF file into multiple files with each file containing one page.

No comments:

Post a Comment