Skip to content

Instantly share code, notes, and snippets.

@frafra
Created October 3, 2013 19:35
Show Gist options
  • Save frafra/6815776 to your computer and use it in GitHub Desktop.
Save frafra/6815776 to your computer and use it in GitHub Desktop.
Split every PDF page in 2 or 4 parts
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2013 Francesco Frassinelli
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pyPdf import PdfFileWriter, PdfFileReader
import copy
half = lambda item: int(item)/2
def splitPages(pdfInput, pdfOutputStream):
pdfOutput = PdfFileWriter()
## 4 parts
#dim = map(half, copy.copy(pdfInput.getPage(0)).mediaBox.upperRight)
#parts = ["upperRight", "lowerRight", "upperLeft", "lowerLeft"]
## 2 parts
original_dim = copy.copy(pdfInput.getPage(0)).mediaBox.upperRight
dim = (original_dim[0], half(original_dim[1]))
parts = ["lowerRight", "upperRight"]
pages = (pdfInput.getPage(i) for i in range(pdfInput.getNumPages()))
for page in pages:
for part in parts:
locals()[part] = copy.copy(page)
setattr(locals()[part].mediaBox, part, dim)
pdfOutput.addPage(locals()[part])
pdfOutput.write(outputStream)
if __name__ == "__main__":
import os, sys
if len(sys.argv) != 2:
print "Usage: %s input.pdf" % sys.argv[0]
sys.exit(1)
if not sys.argv[1].endswith(".pdf"):
print "This is not a pdf file."
sys.exit(1)
pdfInput = PdfFileReader(file(sys.argv[1], "rb"))
if pdfInput.isEncrypted:
pdfInput.decrypt("")
newName = os.path.splitext(sys.argv[1])[0]+"_ebook.pdf"
with open(newName, "wb") as outputStream:
splitPages(pdfInput, outputStream)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment