Skip to content

Instantly share code, notes, and snippets.

@dmr
Created May 21, 2014 05:39
Show Gist options
  • Save dmr/c168646cdac49a108da7 to your computer and use it in GitHub Desktop.
Save dmr/c168646cdac49a108da7 to your computer and use it in GitHub Desktop.
random_reorder_pdf_file takes a PDF file as input and generates 30 pdfs with the same slides but in random order. Helpful to study
#!/usr/bin/env python
import sys
from pyPdf import PdfFileWriter, PdfFileReader
from random import shuffle
import sha
import os
def random_reorder_pdf_file(fn):
fn_base, ext = os.path.splitext(fn)
if ext != '.pdf':
raise RuntimeError('Only PDF files supported. Usage: Pass pdf filename as first parameter')
input_pdf = PdfFileReader(open(fn))
pages = input_pdf.getNumPages()
lst = [[i] for i in range(pages)]
shuffle(lst)
output_pdf = PdfFileWriter()
for page_number_lst in lst:
page_number = page_number_lst[0]
output_pdf.addPage(input_pdf.getPage(page_number))
uid = sha.new(str(lst)).hexdigest()
output_fn = fn_base + uid + '.pdf'
output_file = open(output_fn, "wb")
output_pdf.write(output_file)
output_file.close()
if __name__ == '__main__':
fn = sys.argv[1]
for _ in range(30):
random_reorder_pdf_file(fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment