Created
May 21, 2014 05:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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