This short tutorial explains how to move pages in PDF using Python. It contains steps encompassing the logic to write the application and a running sample code to demonstrate how to rearrange pages in a PDF using Python. You will learn the process to move a page at the end of the page collection or at a particular index according to the needs of the program without using any other third-party tool.
Steps to Reorder Pages in a PDF using Python
- Establish the environment to use Aspose.PDF for Python via .NET to move pages
- Create or load a PDF file using the Document class having a few pages in it for the demo
- Access a reference to a particular page and add it to the end of the collection of the pages
- Delete the page and get the reference to another page
- Insert this page at a particular index and delete it from the original location
- Save the resultant PDF file with reordered pages
These steps describe the process of how to change the order of pages in PDF using Python. The process is commenced by creating or loading a PDF file having multiple pages and then a reference is obtained of a particular page to be moved at the end of the collection of the pages using the add method and then deleted from the source index. In the next steps, the process is described to insert a page in-between the collection at a particular index and then the source page is deleted before saving the final PDF file on the disk.
Code to Move PDF Pages using Python
This code demonstrates the operation to change order of pages in PDF using Python. The Document.pages.add() and Document.pages.insert() methods are used to copy the page at the end of the collection and at a particular index respectively. Similarly, Document.pages.delete() method is used to delete a page by providing the current index of the page in the collection.
This article has taught us how to change order of pages in PDF using Python. If you want to learn the process to merge multiple PDF files, refer to the article on how to merge PDF using Python.