【发布时间】:2017-04-18 14:42:51
【问题描述】:
有没有一种方法可以在 python 中加密 PDF 文件? 一种可能性是压缩 PDF,但还有另一种可能性吗? 谢谢你的帮助 问候 费利克斯
【问题讨论】:
标签: python pdf encryption
有没有一种方法可以在 python 中加密 PDF 文件? 一种可能性是压缩 PDF,但还有另一种可能性吗? 谢谢你的帮助 问候 费利克斯
【问题讨论】:
标签: python pdf encryption
PikePdf 是 python 对 QPDF 的改编,是迄今为止更好的选择。如果您的文件包含非英语语言的文本,这将特别有用。
from pikepdf import Pdf
pdf = Pdf.open(path/to/file)
pdf.save('output_filename.pdf', encryption=pikepdf.Encryption(owner=password, user=password, R=4))
# you can change the R from 4 to 6 for 256 aes encryption
pdf.close()
【讨论】:
你可以使用PyPDF2:
from PyPDF2 import PdfFileReader, PdfFileWriter
with open("input.pdf", "rb") as in_file:
input_pdf = PdfFileReader(in_file)
output_pdf = PdfFileWriter()
output_pdf.appendPagesFromReader(input_pdf)
output_pdf.encrypt("password")
with open("output.pdf", "wb") as out_file:
output_pdf.write(out_file)
如需了解更多信息,请查看PdfFileWriterdocs。
【讨论】:
您可以使用PyPDF2
import PyPDF2
pdfFile = open('input.pdf', 'rb')
# Create reader and writer object
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pdfWriter = PyPDF2.PdfFileWriter()
# Add all pages to writer (accepted answer results into blank pages)
for pageNum in range(pdfReader.numPages):
pdfWriter.addPage(pdfReader.getPage(pageNum))
# Encrypt with your password
pdfWriter.encrypt('password')
# Write it to an output file. (you can delete unencrypted version now)
resultPdf = open('encrypted_output.pdf', 'wb')
pdfWriter.write(resultPdf)
resultPdf.close()
【讨论】:
我强烈推荐 pyAesCrypt 模块。 它基于部分用 C 语言编写的 Cryptography 模块。 该模块非常快,尤其是在高规格计算机中。 您可以期待在高端计算机上对 3 Gb 文件进行 12 秒的加密,所以它确实很快,但不是最好的。
用于加密和解密的一个方法是:
import pyAesCrypt
加密:
pyAesCrypt.encryptFile(inputfile, outputfile, password, bufferSize)
解密:
pyAesCrypt.decryptFile(inputfile, outputfile, password, bufferSize)
由于这不是完整的解释,我建议您完整阅读文档,因为它并不长。 你可以在这里找到它:https://pypi.org/project/pyAesCrypt/
【讨论】:
另一个选项是Aspose.PDF Cloud SDK for Python,它是一个REST API 解决方案。您可以使用从 Amazon S3、DropBox、Google Drive Storage、Google Cloud Storage、Windows Azure Storage、FTP Storage 和 Aspose Cloud Storage 中选择的云存储。
cryptoAlgorithm 采用以下可能的值
import os
import base64
import asposepdfcloud
from asposepdfcloud.apis.pdf_api import PdfApi
from shutil import copyfile
# Get Client key and Client ID from https://cloud.aspose.com
pdf_api_client = asposepdfcloud.api_client.ApiClient(
app_key='xxxxxxxxxxxxxxxxxxxxxxxxxx',
app_sid='xxxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxxx')
pdf_api = PdfApi(pdf_api_client)
temp_folder="Temp"
#upload PDF file to storage
data_file = "C:/Temp/02_pages.pdf"
remote_name= "02_pages.pdf"
pdf_api.upload_file(remote_name,data_file)
out_path = "EncryptedPDF.pdf"
user_password_encoded = base64.b64encode(b'user $^Password!&')
owner_password_encoded = base64.b64encode(b'owner\//? $12^Password!&')
#Encrypte PDF document
response = pdf_api.put_encrypt_document(temp_folder + '/' + out_path, user_password_encoded, owner_password_encoded, asposepdfcloud.models.CryptoAlgorithm.AESX128, file = remote_name)
#download PDF file from storage
response_download = pdf_api.download_file(temp_folder + '/' + out_path)
copyfile(response_download, 'C:/Temp/' + out_path)
print(response)
【讨论】: