2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PDFにパスワードをかける

Posted at

個人情報満載のPDFが見つかり、パスワードをかけておいた方がいいかなと思いたつ。Adobeのアクロバットを買えばいいのだろうけれど、そしてフリーのソフトもあるのだろうけれど、せっかくだからPyPDF2を使ってかけてみる。

PDF_pw.py

import PyPDF2

src_pdf = PyPDF2.PdfFileReader('./**パスワードをかけたいPDF**.pdf')
pass_pdf = './**パスワードをかけた後の出力先**.pdf'
password = '**任意の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)

生成までちょっと間がある印象。パスワードをかけたいPDFとパスワードをかけた後の出力先を同一にすると、上書きされる。でも、なんか失敗してPasswordもわかんないみたいな最悪の事態になるのは嫌だから、別にした方がいいと思う。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?