ABSP, Ch 13, Practice Project #1

My solution to the Practice Project #1 in Chapter 13 of the excellent book “Automate the Boring Stuff with Python” (ABSP)

#! usr/bin/env python3
# by lorenzo - 13/10/2017
# go through every PDF in a folder (and its subfolders)
# and encrypt the PDFs using a password provided on the command line

import os, PyPDF2, sys, send2trash

directory = sys.argv[1]
password = sys.argv[2]

if not os.path.isdir(directory):
    print("Invalid directory!")
    sys.exit()

for folderName, subfolders, filenames in os.walk(directory):
    for filename in filenames:
        if filename.endswith(".pdf"):
            pdfFile = open(os.path.join(folderName, filename), "rb")
            pdfReader = PyPDF2.PdfFileReader(pdfFile)
            pdfWriter = PyPDF2.PdfFileWriter()
            if pdfReader.isEncrypted == True:
                continue
            for pageNum in range(pdfReader.numPages):
                pdfWriter.addPage(pdfReader.getPage(pageNum))
            pdfWriter.encrypt(password)
            pdfResult = open(folderName + "/" + filename[:-4] + "_encrypted.pdf", "wb")
            pdfWriter.write(pdfResult)
            pdfResult.close()
            pdfFile.close()

            pdfFile = open(folderName + "/" + filename[:-4] + "_encrypted.pdf", "rb")
            pdfReader = PyPDF2.PdfFileReader(pdfFile)
            if pdfReader.isEncrypted == True and pdfReader.decrypt(password) == 1:
                print("File " + filename + " encrypted correctly")
                send2trash.send2trash(os.path.join(folderName, filename))
                pdfFile.close()
            else:
                print("File " + filename + " not encrypted correctly")
                pdfFile.close()
#! usr/bin/env python3
# by lorenzo - 13/10/2017
# finds all encrypted PDFs in a folder (and its subfolders)
# and creates a decrypted copy of the PDF using a provided password

import os, PyPDF2, sys, send2trash

directory = sys.argv[1]
password = sys.argv[2]

if not os.path.isdir(directory):
    print("Invalid directory!")
    sys.exit()

for folderName, subfolders, filenames in os.walk(directory):
    for filename in filenames:
        if filename.endswith(".pdf"):
            pdfFile = open(os.path.join(folderName, filename), "rb")
            pdfReader = PyPDF2.PdfFileReader(pdfFile)
            pdfWriter = PyPDF2.PdfFileWriter()
            if pdfReader.isEncrypted == True:
                if pdfReader.decrypt(password) == 1:
                    print("File " + os.path.join(folderName, filename) + " correctly decrypted")
                    pdfReader.decrypt(password)
                    for pageNum in range(pdfReader.numPages):
                        pdfWriter.addPage(pdfReader.getPage(pageNum))
                    pdfResult = open(folderName + "/" + filename[:-4] + "_decrypted.pdf", "wb")
                    pdfWriter.write(pdfResult)
                    pdfFile.close()
                else:
                    print("File: " + os.path.join(folderName, filename) + " not correctly decrypted")

Comments about the code are welcome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s