This short tutorial guides you on how to select pages from PDF and save using Python. It shares all the details required to set the IDE, a list of steps to write the program, and a runnable sample code to select certain pages from PDF using Python and save them in another PDF file. You will also learn different options to extract desired pages from a PDF file with the help of a few API calls only.
Steps to Save Selected Pages from PDF using Python
- Set the IDE to use Aspose.PDF for Python via .NET to select pages from PDF to create new PDF using Python
- Create an object of PdfFileEditor class providing features to extract required pages
- Create an array of page numbers that are to be extracted from the source PDF file
- Call the extract() method by providing the source PDF file name, an array of page numbers, and an output PDF file name
These steps define the process to select some pages from PDF using Python. The PdfFileEditor class object is declared that contains all the rich features to work with a PDF file like adding, removing, appending, and editing the PDF files. The extract() method is used to extract the selected pages by providing the source PDF file name, an array of integers containing the desired page numbers, and the name of the output PDF file that is to be generated.
Code to Export Selected Pages from PDF using Python
This code demonstrates the process to select only few pages from PDF using Python. The PdfFileEditor class from the aspose.pdf.facades namespace is used by declaring its object followed by the creation of an array of integers initialized with the page numbers of the source PDF file. In the end, the extract() method requires the source PDF file name, integer array, and output PDF file name however, you may use other overloaded functions to support reading, writing to and from streams instead of files, and selecting a range of pages instead of random pages.
This article has guided us to save selected pages of PDF using Python. If you want to learn the process to split a PDF file, refer to the article on how to split PDF using Python.