Created
July 14, 2010 02:40
-
-
Save dstrelau/474940 to your computer and use it in GitHub Desktop.
split multi-page PDFs into multiple files (OS X)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Splits an input pdf file into many files, one per page | |
""" | |
import sys | |
import os | |
from CoreGraphics import * | |
inputFN = sys.argv[1] | |
inputDoc = \ | |
CGPDFDocumentCreateWithProvider(\ | |
CGDataProviderCreateWithFilename(inputFN)) | |
if inputDoc: | |
maxPages = inputDoc.getNumberOfPages() | |
print '%s has %d pages' % (inputFN, maxPages) | |
else: | |
sys.exit(2) | |
baseFN = os.path.splitext(os.path.basename(inputFN))[0] | |
pageRect = CGRectMake (0, 0, 612, 792) | |
for pageNum in range(1,maxPages+1): | |
outputFN = '%s-%d.pdf' % (baseFN, pageNum) | |
writeContext = CGPDFContextCreateWithFilename(outputFN, pageRect) | |
print 'Writing page %d' % (pageNum) | |
mediaBox = inputDoc.getMediaBox(pageNum) | |
writeContext.beginPage(mediaBox) | |
writeContext.drawPDFDocument(mediaBox, inputDoc, pageNum) | |
writeContext.endPage() | |
print 'Done: %d file(s) generated.' % maxPages |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment