我是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', '')))发布于 2021-04-04 05:21:27
请参阅以下代码,以获得每个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)如果您有任何问题,请告诉我:)
https://stackoverflow.com/questions/66934220
复制相似问题