Skip to content

Instantly share code, notes, and snippets.

@DrLulz
Created December 13, 2019 15:17
Show Gist options
  • Save DrLulz/be39b3627ee938740aa4203764f85a90 to your computer and use it in GitHub Desktop.
Save DrLulz/be39b3627ee938740aa4203764f85a90 to your computer and use it in GitHub Desktop.
Make individual slides from pdf with six slides per page.
from PyPDF2 import PdfFileWriter, PdfFileReader
from copy import copy
FNAME = '49-urology'
PATH = '/Users/drlulz/Downloads/ABFM/{}.pdf'.format(FNAME)
NEW_PATH = '/Users/drlulz/Downloads/ABFM/Slides/{}.pdf'.format(FNAME)
outputStream = open(NEW_PATH, 'wb')
def go():
with open(PATH, 'rb') as f:
reader = PdfFileReader(f)
writer = PdfFileWriter()
n = reader.getNumPages()
for i in range(n):
page = reader.getPage(i)
s1 = copy(page)
s2 = copy(page)
s3 = copy(page)
s4 = copy(page)
s5 = copy(page)
s6 = copy(page)
s1.mediaBox.lowerLeft = (59, 681) # top left
s1.mediaBox.upperRight = (294, 549) # bottom right
writer.addPage(s1)
s2.mediaBox.lowerLeft = (318, 681) # top left x, y
s2.mediaBox.upperRight = (553, 549) # bottom right
writer.addPage(s2)
###
s3.mediaBox.lowerLeft = (59, 462) # top left
s3.mediaBox.upperRight = (294, 330) # bottom right
writer.addPage(s3)
s4.mediaBox.lowerLeft = (318, 462) # top left x, y
s4.mediaBox.upperRight = (553, 330) # bottom right
writer.addPage(s4)
###
s5.mediaBox.lowerLeft = (59, 243) # top left
s5.mediaBox.upperRight = (294, 111) # bottom right
writer.addPage(s5)
s6.mediaBox.lowerLeft = (318, 243) # top left
s6.mediaBox.upperRight = (553, 111) # bottom right
writer.addPage(s6)
with open(NEW_PATH, 'wb') as out_f:
writer.write(out_f)
go()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment