How to merge PDFs with alternating pages in python


This post will teach you how to create a python script that merges two PDFs into a single file with alternating pages.

Here is a gif showing the script in action:

Image showing odd and even pages being merged in an alternating order.
Merging odd and even pages with the python script.

Why make a script to alternate PDF pages

Many scanners have automatic feeders for scanning multiple pages in one go. Some feeders can scan both sides of a document at the same time, but not all.

If a scanner can only scan one side at a time you’ll have to scan all the odd pages in one pass, flip the pages, and scan the even pages in a second pass.

This will leave you with two files: a PDF with the odd pages, and another with even pages.

To merge them into a single file you’ll have to paste each PDF page manually from one file into another in the right order. This can be time-consuming, especially if your file has many pages.

Adobe Acrobat doesn’t seem to have a way to automate this task, and while there are several websites that can do this, they need you to upload your files and then download the merged PDF document.

This is hard to do with large files and has important security issues: The site might keep a copy of your data, or the merged PDF might come back with malware.

Fortunately, you can solve this problem with a few lines of python code.

What the script does

The script creates a merged PDF from two files named ‘odd.pdf’ and ‘even.pdf’, alternating their pages.

Image showing merging PDFs with alternating pages in python

Coding the script

Below is the code for the script. You can also find the GitHub repository here.

from PyPDF2 import PdfFileReader, PdfFileWriter


class MergePDF():

    def __init__(self):
        self.odd = 'odd.pdf'
        self.even = 'even.pdf'
        self.merged = 'merged.pdf'
        self.open_files()
        self.check_pages()
        if self.check is True:
            self.merge_files()
            print('Finished successfully.')

    def open_files(self):
        self.odd_file = PdfFileReader(open(self.odd, 'rb'))
        self.even_file = PdfFileReader(open(self.even, 'rb'))
        self.odd_pages = self.odd_file.getNumPages()
        self.even_pages = self.even_file.getNumPages()

    def check_pages(self):
        if self.odd_pages == self.even_pages:
            self.check = True
            print("Odd pages are equal to even pages: executing merge.")
        else:
            self.check = False
            print("Odd pages are not equal to even pages: aborting.")

    def merge_files(self):
        self.merge = PdfFileWriter()
        for x in range(self.odd_pages):
            self.merge.addPage(self.odd_file.getPage(x))
            self.merge.addPage(self.even_file.getPage(x))
        self.merge.write(open(self.merged, 'wb'))
        print('Documents merged successfully.')


if __name__ == '__main__':
    oMerge = MergePDF()

How to Install

The script requires the PyPDF2 library, which can be found here, and installed using:

pip install PyPDF2

How to Use

  • Scan all the odd pages and save the PDF file as ‘odd.pdf’.
  • Do the same with the even pages and save the file as ‘even.pdf’.
  • Make sure the number of pages in both files is the same.
  • Move both documents to the same directory as ‘merge.py’.
  • Run the python script.
  • The script will combine both PDFs, alternating the pages and saving the file as ‘merged.pdf’.

Conclusion

If you want to merge two PDFs by alternating their pages, you can use this script, save some time, and keep your data private.

If you decide to try it out, feel free to contact me if you have any problems or suggestions.

You can also check out my other projects here.

Pablo.


Leave a Reply

Your email address will not be published. Required fields are marked *