Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- # A handy python script to reorder the pages of a PDF for duplex scanning
- # from a simplex automatic document feed scanner.
- # When using a single-sided (simplex) scanner to scan a double-sided document,
- # you can first scan the front sides, then the back sides using the ADF.
- # The problem is that the pages are not in the correct order. For a 10 page
- # document, the ordering will be: [1, 3, 5, 7, 9, 10, 8, 6, 4, 2].
- # This script reorders the pages to a new file with the correct ordering.
- #
- # Written by Kareem Shehata, March 2014.
- # This is free and unencumbered software released into the public domain.
- # see http://unlicense.org for details
- # This script requires PyPDF. Be sure to install this package if you don't
- # already have it. "pip install pypdf" should do the trick.
- from pyPdf import PdfFileWriter, PdfFileReader
- # specify the input and output filenames
- # feel free to change the names.
- # Exercise for the reader: change this to use command line parameters or
- # drag and dropping the file onto the script.
- # NB: You can't write to the same filename. I'd suggest renaming after writing
- # the output if you want it to produce the same file on output.
- input_filename = "Scan.pdf"
- output_filename = "Scan_reordered.pdf"
- # create a PDF writer to hold the output
- output_pdf = PdfFileWriter()
- # open up the input file and start reading the PDF
- with open(input_filename, 'rb') as input_file:
- input_pdf = PdfFileReader(input_file)
- # check that we have an even number of pages
- total_pages = input_pdf.getNumPages()
- if (total_pages % 2) != 0:
- print "Cannot de-duplex an odd number of pages!"
- exit(1)
- # Get the pages in pairs, starting with the first and last, and then
- # proceeding to the second and second last, and so on
- for i in range(0, total_pages/2):
- print "Adding page " + str(i)
- output_pdf.addPage(input_pdf.getPage(i))
- print "Adding page " + str(total_pages - i - 1)
- output_pdf.addPage(input_pdf.getPage(total_pages - i - 1))
- # Write the output file. NB: must be done before closing the input file!
- with open(output_filename, 'wb') as output_file:
- output_pdf.write(output_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement