Skip to content
Advertisement

Python: password protect PDFs with random passwords and save file name-password

I am pretty new to Python, what I am looking for is to bulk protect a series of PDFs files within a folder, each file with a unique password randomly generated – these file name-password combinations should then be saved somewhere (potentially CSV file).

Currently using a code that protects all the files within the folder with the same password user-defined. But I cannot manage to protect them with different autogenerated passwords for each PDFs.

thanks a lot in advance for your help

import os 
import pikepdf 
from pikepdf import Pdf

password = 'test'
path = 'path'

def protect(file, password=password):
  
    pdf = Pdf.open(file)    
    pdf.save(os.path.splitext(file)[0] + '_encrypted.pdf', 
             encryption=pikepdf.Encryption(owner=password, user=password, R=4)) 
    pdf.close()
    return

def remove_originals(file):

    if file.endswith(('.pdf', '.PDF')):
        if not file.endswith('_encrypted.pdf'):
            os.remove(file)

#protecting
for folder, subfolders, files in os.walk(path):
    for file in files:
        if file.endswith(('.pdf', '.PDF')):
            protect(os.path.join(folder, file))

#removing originals
for folder, subfolders, files in os.walk(path):
    for file in files:
        if file.endswith(('.pdf', '.PDF')):    
            remove_originals(os.path.join(folder, file))

#renaming the encrypted files to match the original filenames
for folder, subfolders, files in os.walk(path):
    for file in files:
        if file.endswith(('.pdf', '.PDF')):
            os.rename(os.path.join(folder, file), os.path.join(folder, file.replace('_encrypted', '')))

Advertisement

Answer

See the below code to get the desired output with auto-generated password for each pdf:

Edited Implemented in your code:

import os
from random import choice
import pikepdf
from pikepdf import Pdf

path = 'path'
credentials=[]

def protect(file):
    password = ''.join([choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') for i in range(10)])
    pdf = Pdf.open(file)
    pdf.save(os.path.splitext(file)[0] + '_encrypted.pdf',
             encryption=pikepdf.Encryption(owner=password, user=password, R=4))
    pdf.close()
    credentials.append(file.split('\')[1]+","+password)
    return

def remove_originals(file):

    if file.endswith(('.pdf', '.PDF')):
        if not file.endswith('_encrypted.pdf'):
            os.remove(file)

#protecting
for folder, subfolders, files in os.walk(path):
    for file in files:
        if file.endswith(('.pdf', '.PDF')):
            protect(os.path.join(folder, file))

#removing originals
for folder, subfolders, files in os.walk(path):
    for file in files:
        if file.endswith(('.pdf', '.PDF')):
            remove_originals(os.path.join(folder, file))

#renaming the encrypted files to match the original filenames
for folder, subfolders, files in os.walk(path):
    for file in files:
        if file.endswith(('.pdf', '.PDF')):
            os.rename(os.path.join(folder, file), os.path.join(folder, file.replace('_encrypted', '')))
open("credentials.csv","a").writelines(s + 'n' for s in credentials)

Code:

from os import listdir
from random import choice
import pikepdf
from pikepdf import Pdf
Data=[]
path="Pdfs"
OutputFolder="Outpdfs"
pdfs=[ filename for filename in listdir(path) if filename.endswith(".pdf") ]
for pdf in pdfs:
    temppassword=''.join([choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') for i in range(10)])
    with Pdf.open(f"{path}/{pdf}") as pdffile:
        pdffile.save(f"{OutputFolder}/{pdf[:-4]}_encrypted.pdf",encryption=pikepdf.Encryption(owner=temppassword, user=temppassword, R=4))
    Data.append(f"{pdf},{temppassword}")
open("credentials.csv","a").writelines(s + 'n' for s in Data)

Let me know if you have any questions :)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement