In our ever increasing efforts to protect student confidentiality and personal information, we password protect student report cards and test results when emailing them to parents. This helps protect the information in the unlikely event that an email gets sent to the incorrect address.
To do this, we generate the reports from PowerSchool or Google Docs, typically in a Firstname Lastname grade # Progress Report.pdf format. These are put into a folder (in this case, the folder is ES_T3_PDFs) there is also a filedata.csv file that has each file’s password in the first column, the original filename in column 8, and the new filename in column 9.
The python script below then runs, opens each report, creates a new file object, password protects it, and writes it to a new folder (Secure_ES_T3_PDFs).
import PyPDF2
import csv
import sys
#Open csv with password,filename,newfilename
c = open('filedata.csv', 'r')
# Create a reader object to store the data in filedata.csv
reader = csv.reader(c, delimiter=',')
# Process each row of data
count = 0
for row in reader:
# The password located in the first column
password = str(row[0])
# The current (original) filename in "firstname lastname grade # Progress Report - Student_Number" format
currFileName = row[7]
# New filename is the same as original but without the Student_Number
newFileName = row[8]
# Skip the header row
if (password != "Password"): # Skip the first row with "Password" in first column
# print row # every 10th row - just to monitor progress
if (count % 10 == 0):
print(count)
# Open non-encrypted file
pdfFile = open("PasswordProtect/ES_T3_PDFs/"+currFileName, 'rb')
#coverLetter = open("PasswordProtect/coverLetter.pdf", 'rb')
# Create reader and writer objects
pdfReader = PyPDF2.PdfFileReader(pdfFile)
#pdfReader02 = PyPDF2.PdfFileReader(coverLetter)
pdfWriter = PyPDF2.PdfFileWriter()
# The next 2 lines put the welcome letter at the beginning of the new file
#print("Inserting Cover Letter")
#for pageNum in range(pdfReader02.numPages):
# pdfWriter.addPage(pdfReader02.getPage(pageNum))
# Add all pages to writer for each page in input file, add it to the output file
for pageNum in range(pdfReader.numPages):
pdfWriter.addPage(pdfReader.getPage(pageNum))
# Encrypt with password
pdfWriter.encrypt(password)
# Write it to an output file
resultPdf = open("PasswordProtect/Secure_ES_T3_PDFs/"+newFileName, 'wb')
pdfWriter.write(resultPdf)
resultPdf.close()
count += 1