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

2024-04-26 21:32:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: 文件pathinforifpdfospassword
1条回答
网友
1楼 · 发布于 2024-04-26 21:32:02

请参阅下面的代码,以获得每个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)

如果您有任何问题,请告诉我:)

相关问题 更多 >