Skip to content

Instantly share code, notes, and snippets.

@costastf
Created September 20, 2017 14:33
Show Gist options
  • Save costastf/f943f78f59666698137a741bcf26b36b to your computer and use it in GitHub Desktop.
Save costastf/f943f78f59666698137a741bcf26b36b to your computer and use it in GitHub Desktop.
A simple method to spit an open scanned pdf book to single page output.
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
# Copyright (c) 2010 by None
#
# GNU General Public Licence (GPL)
#
# 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 2 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, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
__author__ = '''Costas Tyfoxylos <costas.tyf@gmail.com>'''
__docformat__ = 'plaintext'
__date__ = '02/09/2010'
import sys
import os
from pyPdf import PdfFileWriter, PdfFileReader
def split_pages(filename):
input_document = open(filename, 'rb')
pdf_name = input_document.name
pdf = PdfFileReader(input_document)
out_pdf = PdfFileWriter()
for index in range(pdf.numPages):
page = pdf.getPage(index)
width = page.mediaBox[2]
height = page.mediaBox[3]
if width > height:
firstPdf = PdfFileReader(input_document)
secondPdf = PdfFileReader(input_document)
firstPage = firstPdf.getPage(index)
secondPage = secondPdf.getPage(index)
firstPage.cropBox.lowerLeft = (0, 0)
firstPage.cropBox.upperRight = (width / 2 + 10, height)
secondPage.cropBox.lowerLeft = (width / 2 - 10, 0)
secondPage.cropBox.upperRight = (width, height)
out_pdf.addPage(firstPage)
out_pdf.addPage(secondPage)
else:
out_pdf.addPage(page)
out_put_file = open("FullPage {filename}".format(filename=pdf_name), 'wb')
out_pdf.write(out_put_file)
out_put_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment