Open encrypted PDF file by trying passwords from a word list
Looking at the documentation of PdfFileReader.decrypt It states that it return 0 if the file fails or 1 if it succeeds.

You can also use isEncrypted to check if the file was encrypted in the first place.

So you can turn the loop around. First open the pdf file see if it was encrypted at all and if so try to call decrypt with every password in your dictionary.