Skip to content

Instantly share code, notes, and snippets.

@dinigo
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinigo/ae358bd01ae5f87e3c2f to your computer and use it in GitHub Desktop.
Save dinigo/ae358bd01ae5f87e3c2f to your computer and use it in GitHub Desktop.
Split pdf pages wich contains slides so that every page contains just one slide. It only works with two slides per page, either horizontal or vertical.
#!/usr/bin/env python
# Based on http://unix.stackexchange.com/a/12483
# To use it you have to give it execution privilleges
# with the command:
# chmod u+x split.py
# Then you can use it like this:
# ./split.py <slides.pdf >split.pdf
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()
for p in [input.getPage(i) for i in range(0,input.getNumPages())]:
u = copy.copy(p)
d = copy.copy(p)
(xur, yur) = p.mediaBox.upperRight
(xll, yll) = p.mediaBox.lowerLeft
if xll > yll:
# horizontal
u.mediaBox.upperRight = (xur/2, yur)
d.mediaBox.lowerLeft = (xur/2, yll)
else:
# vertical
u.mediaBox.lowerLeft = (xll, yur/2)
d.mediaBox.upperRight = (xur, yur/2)
output.addPage(u)
output.addPage(d)
output.write(sys.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment