Home > Back-end >  How to add blank pages at the end of PDF file?
How to add blank pages at the end of PDF file?

Time:07-01

I am trying to compare two PDFs. I want to add blank pages to the end of the PDF which has less number of pages, so that the number of pages in both PDFs is same. My question is how to compare and add blank pages to the PDF using python?

CodePudding user response:

There are a number of libraries that can help you get what you want. PyPDF2 is one of the popular ones, and you can use two of the functions provided by this library to reach your goal.

from PyPDF2 import PdfReader

reader = PdfReader("example.pdf")
number_of_pages = len(reader.pages)

You can figure out the number of pages in your pdf using the above code and calculate the difference. After that, you can use the add_blank_page function to add the number of blank pages to your pdf. Here's the doc for the add_blank_page function: https://pypdf2.readthedocs.io/en/latest/modules/PdfWriter.html#PyPDF2.PdfWriter.add_blank_page

The library has very extensive documentation and can help you, so check it out if you get stuck: https://pypdf2.readthedocs.io/en/latest/

  • Related