Python:使用随机密码保护PDF,并保存文件名密码



我对Python很陌生,我想要的是批量保护文件夹中的一系列PDF文件,每个文件都有一个随机生成的唯一密码-这些文件名-密码组合应该保存在某个地方(可能是CSV文件(。

当前使用的代码使用用户定义的相同密码保护文件夹中的所有文件。但我无法为每个PDF自动生成不同的密码来保护它们。

提前非常感谢的帮助

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', '')))

请参阅下面的代码,以获得所需的输出,并为每个pdf自动生成密码:

编辑在您的代码中实现:

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)

代码:

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)

如果你有任何问题,请告诉我:(

最新更新