Delete PDF Pages in Python
You can delete pages from a PDF file using Aspose.PDF for Python via .NET. To delete a particular page, use the PageCollection of a Document.
Use this workflow when you need to remove unwanted pages from a PDF before sharing, archiving, or combining documents.
Delete Page from PDF File
Aspose.PDF for Python via .NET deletes page 2 from the input PDF and saves the updated document to a new file. This feature is helpful for removing unwanted or sensitive pages without altering the rest of the document.
- Load the input PDF as a
Document. - Delete the page using the
PageCollection. - Call the
Document.save()method to save the updated PDF file.
The following code snippet shows how to delete a particular page from the PDF file using Python.
import aspose.pdf as ap
def delete_page(input_file_name: str, output_file_name: str) -> None:
document = ap.Document(input_file_name)
document.pages.delete(2)
document.save(output_file_name)
Delete Multiple Pages from a PDF Document
Deleting multiple pages allows you to remove a set of specified pages in a single operation, which is more efficient than deleting pages one by one. The resulting PDF is saved to a new file, preserving the original document.
- Load the input PDF as a
Document. - Delete the pages listed in the pages array using the
PageCollection. - Save the updated
Documentto a new file.
import aspose.pdf as ap
def delete_multiple_pages(input_file_name: str, output_file_name: str) -> None:
document = ap.Document(input_file_name)
# Example: delete pages 2, 3, and 4.
pages = [2, 3, 4]
document.pages.delete(pages)
document.save(output_file_name)