I found a PDF full of personal information and thought it would be better to put a password on it. I wonder if I should buy Adobe's acrobatics, and maybe there is free software, but that's why I try using PyPDF2.
PDF_pw.py
import PyPDF2
src_pdf = PyPDF2.PdfFileReader('./**PDF you want to password**.pdf')
pass_pdf = './**Output destination after applying a password**.pdf'
password = '**Any Password**'
dst_pdf = PyPDF2.PdfFileWriter()
dst_pdf.cloneReaderDocumentRoot(src_pdf)
d = {key: src_pdf.documentInfo[key] for key in src_pdf.documentInfo.keys()}
dst_pdf.addMetadata(d)
dst_pdf.encrypt(password)
with open(pass_pdf, 'wb') as f:
dst_pdf.write(f)
Impression that there is a little time until generation. If the PDF to which you want to apply a password and the output destination after applying a password are the same, it will be overwritten. However, I don't want to be in the worst situation where I don't know the password if something goes wrong, so I think it's better to set it aside.