SHARE
TWEET

Double sided scanning from a single sided scanner

indigofire Mar 16th, 2014 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/python
  2. # A handy python script to reorder the pages of a PDF for duplex scanning
  3. # from a simplex automatic document feed scanner.
  4. # When using a single-sided (simplex) scanner to scan a double-sided document,
  5. # you can first scan the front sides, then the back sides using the ADF.
  6. # The problem is that the pages are not in the correct order. For a 10 page
  7. # document, the ordering will be: [1, 3, 5, 7, 9, 10, 8, 6, 4, 2].
  8. # This script reorders the pages to a new file with the correct ordering.
  9. #
  10. # Written by Kareem Shehata, March 2014.
  11. # This is free and unencumbered software released into the public domain.
  12. # see http://unlicense.org for details
  13.  
  14. # This script requires PyPDF. Be sure to install this package if you don't
  15. # already have it. "pip install pypdf" should do the trick.
  16. from pyPdf import PdfFileWriter, PdfFileReader
  17.  
  18. # specify the input and output filenames
  19. # feel free to change the names.
  20. # Exercise for the reader: change this to use command line parameters or
  21. # drag and dropping the file onto the script.
  22. # NB: You can't write to the same filename. I'd suggest renaming after writing
  23. # the output if you want it to produce the same file on output.
  24. input_filename = "Scan.pdf"
  25. output_filename = "Scan_reordered.pdf"
  26.  
  27. # create a PDF writer to hold the output
  28. output_pdf = PdfFileWriter()
  29.  
  30. # open up the input file and start reading the PDF
  31. with open(input_filename, 'rb') as input_file:
  32.     input_pdf = PdfFileReader(input_file)
  33.  
  34.     # check that we have an even number of pages
  35.     total_pages = input_pdf.getNumPages()
  36.     if (total_pages % 2) != 0:
  37.         print "Cannot de-duplex an odd number of pages!"
  38.         exit(1)
  39.  
  40.     # Get the pages in pairs, starting with the first and last, and then
  41.     # proceeding to the second and second last, and so on
  42.     for i in range(0, total_pages/2):
  43.         print "Adding page " + str(i)
  44.         output_pdf.addPage(input_pdf.getPage(i))
  45.         print "Adding page " + str(total_pages - i - 1)
  46.         output_pdf.addPage(input_pdf.getPage(total_pages - i - 1))
  47.  
  48.     # Write the output file. NB: must be done before closing the input file!
  49.     with open(output_filename, 'wb') as output_file:
  50.         output_pdf.write(output_file)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top