lundi 23 avril 2018

Randomly shuffle the pages of a PDF file using pyPDF or PyPDF2

I'm not very experienced in programming. What I'm trying to do is to randomly shuffle the pages of a pdf and output it to another pdf.

Searching online I found the following two solutions (source 1, source 2):

#!/usr/bin/env python2
import random, sys

from PyPDF2 import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()
pages = range(input.getNumPages())
random.shuffle(pages)
for i in pages:
     output.addPage(input.getPage(i))
output.write(sys.stdout)

And this one:

#!/usr/bin/python

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()))
random.shuffle(pages)

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

# add the new sequence of pages to output pdf
for page in pages:
    output.addPage(input1.getPage(page))

# write the output pdf to file
outputStream = file(sys.argv[1]+'-mixed.pdf','wb')
output.write(outputStream)
outputStream.close()

I tried both (and both with PyPDF2 and pyPdf) and both indeed create a new pdf file, but this file is simply empty (and has 0KB) (when I enter, let's say "shuffle.py new.pdf").

I'm using PyCharm and one problem I encounter (and not really understand) is that it says: "Cannot find reference 'PdfFileWriter'".

PyCharm tells me that it cannot find the reference

I would appreciate any help understanding what I'm doing wrong :)




Aucun commentaire:

Enregistrer un commentaire