Skip to content

Instantly share code, notes, and snippets.

@roycechua
Last active October 22, 2019 06:48
Show Gist options
  • Select an option

  • Save roycechua/e5c662b1d0837f6dedea569cab40318c to your computer and use it in GitHub Desktop.

Select an option

Save roycechua/e5c662b1d0837f6dedea569cab40318c to your computer and use it in GitHub Desktop.
PyPDF2 code to append/Stitch two PDF files into one single file. This is just a proof of the idea but the more optimal thing to do would be to determine the total number of pages per file.
from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.pdf import PageObject
writer = PdfFileWriter()
reader = PdfFileReader(open("1.2.1.13 Lab - Research Computer Components.pdf",'rb'))
for i in range(3):
page = reader.getPage(i)
writer.addPage(page)
sup_reader = PdfFileReader(open("Week1_Introduction to Machine Learning and Toolkit.pdf",'rb'))
for i in range(3):
page = sup_reader.getPage(i)
writer.addPage(page)
with open('out.pdf', 'wb') as f:
writer.write(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment