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
JavaScript
x
39
39
1
import os
2
import pikepdf
3
from pikepdf import Pdf
4
5
password = 'test'
6
path = 'path'
7
8
def protect(file, password=password):
9
10
pdf = Pdf.open(file)
11
pdf.save(os.path.splitext(file)[0] + '_encrypted.pdf',
12
encryption=pikepdf.Encryption(owner=password, user=password, R=4))
13
pdf.close()
14
return
15
16
def remove_originals(file):
17
18
if file.endswith(('.pdf', '.PDF')):
19
if not file.endswith('_encrypted.pdf'):
20
os.remove(file)
21
22
#protecting
23
for folder, subfolders, files in os.walk(path):
24
for file in files:
25
if file.endswith(('.pdf', '.PDF')):
26
protect(os.path.join(folder, file))
27
28
#removing originals
29
for folder, subfolders, files in os.walk(path):
30
for file in files:
31
if file.endswith(('.pdf', '.PDF')):
32
remove_originals(os.path.join(folder, file))
33
34
#renaming the encrypted files to match the original filenames
35
for folder, subfolders, files in os.walk(path):
36
for file in files:
37
if file.endswith(('.pdf', '.PDF')):
38
os.rename(os.path.join(folder, file), os.path.join(folder, file.replace('_encrypted', '')))
39
Advertisement
Answer
See the below code to get the desired output with auto-generated password for each pdf:
Edited Implemented in your code:
JavaScript
1
42
42
1
import os
2
from random import choice
3
import pikepdf
4
from pikepdf import Pdf
5
6
path = 'path'
7
credentials=[]
8
9
def protect(file):
10
password = ''.join([choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') for i in range(10)])
11
pdf = Pdf.open(file)
12
pdf.save(os.path.splitext(file)[0] + '_encrypted.pdf',
13
encryption=pikepdf.Encryption(owner=password, user=password, R=4))
14
pdf.close()
15
credentials.append(file.split('\')[1]+","+password)
16
return
17
18
def remove_originals(file):
19
20
if file.endswith(('.pdf', '.PDF')):
21
if not file.endswith('_encrypted.pdf'):
22
os.remove(file)
23
24
#protecting
25
for folder, subfolders, files in os.walk(path):
26
for file in files:
27
if file.endswith(('.pdf', '.PDF')):
28
protect(os.path.join(folder, file))
29
30
#removing originals
31
for folder, subfolders, files in os.walk(path):
32
for file in files:
33
if file.endswith(('.pdf', '.PDF')):
34
remove_originals(os.path.join(folder, file))
35
36
#renaming the encrypted files to match the original filenames
37
for folder, subfolders, files in os.walk(path):
38
for file in files:
39
if file.endswith(('.pdf', '.PDF')):
40
os.rename(os.path.join(folder, file), os.path.join(folder, file.replace('_encrypted', '')))
41
open("credentials.csv","a").writelines(s + 'n' for s in credentials)
42
Code:
JavaScript
1
15
15
1
from os import listdir
2
from random import choice
3
import pikepdf
4
from pikepdf import Pdf
5
Data=[]
6
path="Pdfs"
7
OutputFolder="Outpdfs"
8
pdfs=[ filename for filename in listdir(path) if filename.endswith(".pdf") ]
9
for pdf in pdfs:
10
temppassword=''.join([choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') for i in range(10)])
11
with Pdf.open(f"{path}/{pdf}") as pdffile:
12
pdffile.save(f"{OutputFolder}/{pdf[:-4]}_encrypted.pdf",encryption=pikepdf.Encryption(owner=temppassword, user=temppassword, R=4))
13
Data.append(f"{pdf},{temppassword}")
14
open("credentials.csv","a").writelines(s + 'n' for s in Data)
15
Let me know if you have any questions :)