How to Delete Pages from PDF Files with Python

Learn how to Delete Pages from PDF Files with Python by calling Split PDF API Tool by pdfRest.
Share this page

Why Delete PDF Pages with Python?

The pdfRest Split PDF API Tool is a powerful resource for developers who need to programmatically delete pages from PDF documents. This could be useful in a variety of scenarios, including removing sensitive content from reports.

By using Python, a widely-used programming language, developers can easily integrate this functionality into their applications or workflows.

Delete PDF Pages with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

split_pdf_endpoint_url = 'https://api.pdfrest.com/split-pdf'

# The /split-pdf endpoint can take one PDF file or id as input.
# This sample takes one PDF file that has at least 5 pages and splits it into two documents when given two page ranges.

# Create a list of tuples for data that will be sent to the request
split_request_data = []
split_request_data.append(('file',('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf')))
split_request_data.append(('pages', '1-3,5-last'))
split_request_data.append(('output', 'example_splitPdf_out'))

mp_encoder_splitPdf = MultipartEncoder(
    fields=split_request_data
)

# Let's set the headers that the split-pdf endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_splitPdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to split-pdf endpoint...")
response = requests.post(split_pdf_endpoint_url, data=mp_encoder_splitPdf, headers=headers)

print("Response status code: " + str(response.status_code))

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.

This code is sourced from the pdf-rest-api-samples repository on GitHub.

Breaking Down the Code

The code above demonstrates how to use the pdfRest Split PDF API tool with Python. Let's break down the key parts:

split_request_data.append(('file',('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf')))

This line appends a tuple to the request data list, containing the file to be split. The file is opened in binary read mode ('rb') and is specified to be of 'application/pdf' MIME type.

split_request_data.append(('pages', '1-3,5-last'))

This line appends page ranges to the request data list. The API will split the PDF into a single document containing all pages except page 4, which has the effect of deleting page 4.

split_request_data.append(('output', 'example_splitPdf_out'))

This line specifies the base name for the output files that will be generated by the split operation.

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_splitPdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

The headers include the 'Accept' header, which indicates that the client expects a JSON response. The 'Content-Type' is set automatically by the MultipartEncoder. The 'Api-Key' should be replaced with your actual API key from pdfRest.

response = requests.post(split_pdf_endpoint_url, data=mp_encoder_splitPdf, headers=headers)

This sends a POST request to the split-pdf endpoint with the encoded data and headers. The response is then checked for success and printed out.

Beyond the Tutorial

In this tutorial, you've learned how to send an API call to the pdfRest Split PDF tool using Python. This allows you to automate the process for deleting pages from a PDF and could be integrated into larger systems for document management or processing.

To further explore the capabilities of pdfRest API Tools, you can demo all of them in the API Lab. For more detailed information, visit the API Reference documentation.

Note that this is an example of a multipart API call. Code samples using JSON payloads for the same operation can be found in our GitHub repository.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.