Skip to content

Instantly share code, notes, and snippets.

@tuchella
Created December 18, 2016 14:17
Show Gist options
  • Save tuchella/e80b916cc611a38ef3567fe5aadd3eb7 to your computer and use it in GitHub Desktop.
Save tuchella/e80b916cc611a38ef3567fe5aadd3eb7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from PyPDF2 import PdfFileWriter, PdfFileReader
import sys, getopt, os
def parse_range(page):
start, end = page.split('-')
start = int(start)
end = int(end)
return range(start, end + 1)
def parse_pages(pages):
pages = remove_whitespace(pages)
parsed = []
for page in pages.split(','):
if '-' in page:
page_range = parse_range(page)
parsed.extend(page_range)
else:
parsed.append(int(page))
return parsed
def remove_whitespace(s):
return "".join(s.split())
def generate_output_name(pages, inputfile):
ext_index = inputfile.rindex('.')
return inputfile[0:ext_index] + '_' + pages + inputfile[ext_index:]
def copy_pages(pages, inputpath, outputpath):
if os.path.exists(outputpath):
raise Exception('file at ' + outputpath + ' already exists')
print 'Creating pdf \'' + outputpath + '\' from pages ' + str(pages) + ' of \'' + inputpath + '\'...'
output = PdfFileWriter()
input = PdfFileReader(open(inputpath, 'rb'))
for page in pages:
# we subtract -1 here as the library uses zero-based
# index for the pages put we use one-based
# page numbers as parameter
output.addPage(input.getPage(page-1))
out = file(outputpath, 'wb')
output.write(out)
out.close()
print 'done...'
def main(argv):
inputfile = ''
outputfile = ''
pages = ''
opts, args = getopt.getopt(argv, 'i:o:p:h', ['help'])
for opt, val in opts:
if opt == '-h':
print './split_pdf -i input_file [-o outputfile] -p pages'
print 'pages is a comma separated list of indivudual page numbers or page range (e.g. 1,2,3 or 1,2,10-15)'
elif opt == '-i':
inputfile = val
elif opt == '-o':
outputfile = val
elif opt == '-p':
pages = val
if not outputfile:
outputfile = generate_output_name(pages, inputfile)
pages = parse_pages(pages)
copy_pages(pages, inputfile, outputfile)
# opens the create file with system viewer
os.system('cygstart ' + outputfile)
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment