Python PikePDF – Extract page from PDF file

In this post we will be creating a Python script that will extract a single page from a PDF file. To work with PDF files and achieve our goal, we’ll utilize the PikePDF library designed specifically for this purpose. Its simple yet powerful functions enable easy merging, splitting, and manipulation of PDF documents.

See the snippet of Python code below where we use the PikePDF library to extract a single page from an existing PDF file. We start with a with statement which opens the PDF file using PikePDF’s ‘open()’ function. We then select the first page of the PDF using the ‘pages’ attribute of the ‘pdf’ object. using PikePDF’s new() function, we create a new and empty ‘Pdf’ object where we will later store the contents of our page. The ‘append()‘ method is then used to add the selected page to the new_pdf object. This is saved to a new file named “extracted_page.pdf”.

import pikepdf

with pikepdf.open("file.pdf") as pdf:
    page = pdf.pages[0] 
    new_pdf = pikepdf.Pdf.new()
    new_pdf.pages.append(page)
    new_pdf.save("extracted_page.pdf")

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply