2020年3月1日日曜日

開発環境

退屈なことはPythonにやらせよう ―ノンプログラマーにもできる自動化処理プログラミング (Al Sweigart(著)、相川 愛三(翻訳)、オライリージャパン)の第Ⅱ部(処理の自動化)、13章(PDFファイルとWordファイル文書)、13.6(演習プロジェクト)、13.6.1(PDFをまとめて暗号化)の解答を求めてみる。

コード

encrypt.py

#!/usr/bin/env python3
import argparse
import os
import PyPDF2

parser = argparse.ArgumentParser()
parser.add_argument('password', type=str)
args = parser.parse_args()
password: str = args.password

for foldername, subfolders, filenames in os.walk(os.getcwd()):
    for filename in filenames:
        if not filename.endswith('.pdf'):
            continue
        filename = os.path.join(foldername, filename)
        with open(filename, 'rb') as in_file:
            reader = PyPDF2.PdfFileReader(in_file)
            if reader.isEncrypted:
                continue
            writer = PyPDF2.PdfFileWriter()
            for page_num in range(reader.numPages):
                writer.addPage(reader.getPage(page_num))
            writer.encrypt(password)
            filename_encrypted: str = os.path.join(
                foldername, f'{filename[:-4]}_encrypted.pdf')
            with open(filename_encrypted, 'wb') as out_file:
                writer.write(out_file)

decrypt.py

#!/usr/bin/env python3
import argparse
import os
import PyPDF2

parser = argparse.ArgumentParser()
parser.add_argument('password', type=str)
args = parser.parse_args()
password: str = args.password

for foldername, subfolders, filenames in os.walk(os.getcwd()):
    for filename in filenames:
        if not filename.endswith('.pdf'):
            continue
        filename = os.path.join(foldername, filename)
        with open(filename, 'rb') as in_file:
            reader = PyPDF2.PdfFileReader(in_file)
            if not reader.isEncrypted:
                continue
            if reader.decrypt(password) == 0:
                print(f'{os.path.basename(filename)}: Invalid Password')
                continue
            writer = PyPDF2.PdfFileWriter()
            for page_num in range(reader.numPages):
                writer.addPage(reader.getPage(page_num))
            filename_decripted: str = os.path.join(
                foldername, filename.replace('encrypted', 'decrypted'))
            with open(filename_decripted, 'wb') as out_file:
                writer.write(out_file)

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% tree
.
├── encrypt.py
├── tmp.pdf
├── tmp1
│   ├── c.pdf
│   └── tmp3
│       └── a.pdf
└── tmp2
    ├── a.pdf
    └── b.pdf

3 directories, 6 files
% ./encrypt.py 
usage: encrypt.py [-h] password
encrypt.py: error: the following arguments are required: password
% ./encrypt.py pass    
% tree
.
├── encrypt.py
├── tmp.pdf
├── tmp1
│   ├── c.pdf
│   ├── c_encrypted.pdf
│   └── tmp3
│       ├── a.pdf
│       └── a_encrypted.pdf
├── tmp2
│   ├── a.pdf
│   ├── a_encrypted.pdf
│   ├── b.pdf
│   └── b_encrypted.pdf
└── tmp_encrypted.pdf

3 directories, 11 files
% open tmp_encrypted.pdf 
% open tmp1/tmp3/a_encrypted.pdf 
…
% ./decrypt.py 
usage: decrypt.py [-h] password
decrypt.py: error: the following arguments are required: password
% ./decrypt.py abcde
tmp_encrypted.pdf: Invalid Password
a_encrypted.pdf: Invalid Password
b_encrypted.pdf: Invalid Password
c_encrypted.pdf: Invalid Password
a_encrypted.pdf: Invalid Password
% ./decrypt.py pass
% tree
.
├── decrypt.py
├── encrypt.py
├── tmp.pdf
├── tmp1
│   ├── c.pdf
│   ├── c_decrypted.pdf
│   ├── c_encrypted.pdf
│   └── tmp3
│       ├── a.pdf
│       ├── a_decrypted.pdf
│       └── a_encrypted.pdf
├── tmp2
│   ├── a.pdf
│   ├── a_decrypted.pdf
│   ├── a_encrypted.pdf
│   ├── b.pdf
│   ├── b_decrypted.pdf
│   └── b_encrypted.pdf
├── tmp_decrypted.pdf
└── tmp_encrypted.pdf

3 directories, 17 files
% open tmp_encrypted.pdf 
% open tmp_decrypted.pdf
% open tmp2/a*.pdf
% 

0 コメント:

コメントを投稿