首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python: password使用随机密码保护PDF,并保存文件名-密码

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

Stack Overflow用户
提问于 2021-04-04 01:50:34
回答 1查看 326关注 0票数 0

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

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

非常感谢您的帮助

代码语言:javascript
复制
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', '')))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-04 05:21:27

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

在您的代码中实现的编辑:

代码语言:javascript
复制
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)

代码:

代码语言:javascript
复制
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)

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66934220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档