Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for a series of commands to randomize the order of pages in an existing PDF document.

share|improve this question
See also How can I shuffle pages from a PDF file in a random order?  (on Unix&Linux) – G-Man Jun 14 '15 at 21:38

Turns out, there's a nice python library pyPDF which can be used in the following script to randomize the order of pages in a PDF document.

The script below, call it mixpdf, creates a copy of an input PDF file with randomly reordered pages when called by the statement mixpdf myinputfile.pdf.


import sys
import random

from pyPdf import PdfFileWriter, PdfFileReader

# read input pdf and instantiate output pdf
output = PdfFileWriter()
input1 = PdfFileReader(file(sys.argv[1],"rb"))

# construct and shuffle page number list
pages = list(range(input1.getNumPages()))

# display new sequence
print 'Reordering pages according to sequence:'
print pages

# add the new sequence of pages to output pdf
for page in pages:

# write the output pdf to file
outputStream = file(sys.argv[1]+'-mixed.pdf','wb')
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.