Reviewing Papers: Adding a note Page after each page in PDF programmatically

Written on September 13, 2022

I had this brilliant idea of adding a cornell style note page after every single page of a paper I am reviewing. Because why not, right? Everyone loves tinkering around the reviewing process instead of actually reviewing, so … there.

Turns out that it is fairly simple to do this programmatically using python these days. You need a PyPi packaged called pypdf2 package before, and a template style cornell page that you can generate from here.

An image with two PDF thumbnail images side by side. The first image shows the PDF version of this website, with an arrow stretching from it to the second image. The second image is of cornel style note page.

You are golden at this point. Here are the steps:

1. Setting Up Environment

cd <your favorite directory>

# create a virtual environment
python3 -m venv venv

# activate the virtual environment
source venv/bin/activate

# install the PyPDF2 package
pip install pypdf2

2. Script to Add Pages

from sys import argv
from PyPDF2 import PdfMerger, PdfFileReader
import sys

def add_pages(input_pdf_path: str, template_pdf_path: str):
    input_pdf = open(input_pdf_path, "rb")
    page_count: int = PdfFileReader(input_pdf_path).getNumPages()
    template = open(template_pdf_path, "rb")
    merger = PdfMerger()

    print("original page count: " + str(page_count))
    for i in range(0, page_count):
        print("adding templatr page at "+ str(2 * i + 1))
        merger.append(fileobj=input_pdf, pages=(i, i+1))
        append_index = (2 * i) + 1
        merger.merge(position=append_index, fileobj=template, pages=(0, 1))

    template.close()
    input_pdf.close()
    write_file(input_pdf_path, merger)

def write_file(input_pdf_path: str, merger: PdfMerger):
    with open(input_pdf_path+"merged.pdf", "wb") as out:
        merger.write(out)
    merger.close()

def main():

    print(sys.argv)
    if len(sys.argv) != 3:
        print("Please specify the absolute paths to the input file and template file")
        print("Format: add_comment_page input_pdf_path template_pdf_path ")
        exit()
    input_pdf_path = sys.argv[1]
    template_pdf_path = sys.argv[2]
    add_pages(input_pdf_path, template_pdf_path)

if __name__ == "__main__":
    main()

Aaand that’s all folks!

Code, Helper Script, requirements.txt for pip, and Cornell Style notepage as PDF are available in this GitHub repo link as well, so get going!


First, click on "Comments" below to view/post comments.
To comment as guest, click on the field "Name". The option to do so will become visible.
লগইন ছাড়াই কমেন্ট করতে নাম এ ক্লিক করুন, দেখবেন তার নিচেই আছে অতিথি হিসাবে কমেন্ট করার অপশন।