Working with PDFs in Python: Inserting, Deleting, and Reordering Pages
This article is the third in a series on working with PDFs in Python:
Introduction
This article is part three of a little series on working with PDFs in Python. In the previous articles we gave an introduction into reading PDF documents using Python. So far you have learned how to manipulate existing PDFs, and to read and extract the content – both text and images. Furthermore, we have discussed splitting documents into its single pages, as well as adding watermarks and barcodes.
Now in this article we will go one step further and demonstrate how to rearrange a PDF document in a few different ways.
Deleting Pages with pdfrw
Deleting individual pages from a PDF file is as simple as the following:
- Read a PDF as an input file
- Write selected pages to a new PDF as an output file
The following example removes the first two pages from a PDF document. Using the pdfrw library, the file is read with the help of the PdfReader()
class first. Except for both the first and second page, each page is added to the output file using the addpage()
method, and then written to disk