pikepdf

10.5.1 · active · verified Thu Apr 09

pikepdf is a Python library that enables reading, writing, repairing, and transforming PDF files, leveraging the powerful `qpdf` C++ library. It offers comprehensive PDF manipulation capabilities, including merging, splitting, compressing, and extracting data. Currently at version 10.5.1, it follows a regular release cadence with several minor and patch releases per year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a new PDF, add pages to it, and then open and modify an existing PDF file using `pikepdf`. The `Pdf` object should always be used within a `with` statement for proper resource management.

from pikepdf import Pdf, Page
import os

# Create a new PDF and add a blank page
output_filename = "my_first_pikepdf.pdf"
with Pdf.new() as pdf:
    pdf.add_blank_page()
    pdf.save(output_filename)

print(f"Created {output_filename} with one blank page.")

# Open an existing PDF, add another page, and save it to a new file
modified_filename = "my_modified_pikepdf.pdf"
with Pdf.open(output_filename) as pdf:
    pdf.add_blank_page()
    print(f"PDF now has {len(pdf.pages)} pages.")
    pdf.save(modified_filename)

print(f"Modified PDF saved to {modified_filename}.")

# Clean up generated files (optional)
# os.remove(output_filename)
# os.remove(modified_filename)

view raw JSON →