output_settings.md

2023/10/08 categories:Code| tags:Code|

記事に戻る

# -*- coding: utf-8 -*-
import fitz
import random
import string
import subprocess
from pathlib import Path
from PyQt6 import QtCore, QtGui, QtWidgets

class OutputSettings(QtWidgets.QGroupBox):
    def __init__(self):
        super().__init__()
        self.setTitle('出力設定')

        self.folder = QtWidgets.QLineEdit('output')
        self.folder.setText('outputs')
        self.open_folder = QtWidgets.QPushButton('フォルダを開く')

        self.owner_pw_edit = QtWidgets.QLineEdit()
        self.owner_pw_enable = QtWidgets.QCheckBox('オーナーパスワード')
        self.owner_pw_generate = QtWidgets.QPushButton('ランダムな文字列を生成')

        self.user_pw_edit = QtWidgets.QLineEdit()
        self.user_pw_enable = QtWidgets.QCheckBox('ユーザーパスワード')
        self.user_pw_generate = QtWidgets.QPushButton('ランダムな文字列を生成')

        self.encryption_text = QtWidgets.QComboBox()
        self.encryption_text.addItems([
            '読み込んだPDFの暗号化を保持',
            '暗号化無し',
            '40 bit RC4',
            '128 bit RC4',
            '128 bit AES',
            '256 bit AES'
        ])
        self.encryption_text.setCurrentText('256 bit AES')

        self.PDF_PERM_PRINT = QtWidgets.QCheckBox('印刷')
        self.PDF_PERM_ASSEMBLE = QtWidgets.QCheckBox('文書アセンブリ')
        self.PDF_PERM_MODIFY = QtWidgets.QCheckBox('変更')
        self.PDF_PERM_COPY = QtWidgets.QCheckBox('内容のコピー')
        self.PDF_PERM_ACCESSIBILITY = QtWidgets.QCheckBox('アクセシビリティのための内容の抽出')
        self.PDF_PERM_PRINT_HQ = QtWidgets.QCheckBox('高品質の印刷')
        self.PDF_PERM_FORM = QtWidgets.QCheckBox('フォームの記入と署名')
        self.PDF_PERM_ANNOTATE = QtWidgets.QCheckBox('注釈やフォームの追加、変更')

        self.PDF_PERM_PRINT.setChecked(True)
        self.PDF_PERM_COPY.setChecked(True)
        self.PDF_PERM_ACCESSIBILITY.setChecked(True)
        self.PDF_PERM_PRINT_HQ.setChecked(True)
        self.PDF_PERM_ANNOTATE.setChecked(True)

        l1 = QtWidgets.QLabel('暗号化')
        l1.setSizePolicy(QtWidgets.QSizePolicy.Policy.Maximum, QtWidgets.QSizePolicy.Policy.Maximum)

        w0 = self.hboxlayout_widget([self.folder, self.open_folder])
        w1 = self.hboxlayout_widget([self.owner_pw_enable, self.owner_pw_edit, self.owner_pw_generate])
        w2 = self.hboxlayout_widget([self.user_pw_enable, self.user_pw_edit, self.user_pw_generate])
        w3 = self.hboxlayout_widget([l1, self.encryption_text])
        w4 = self.hboxlayout_widget([self.PDF_PERM_PRINT, self.PDF_PERM_ASSEMBLE, self.PDF_PERM_MODIFY, self.PDF_PERM_COPY])
        w5 = self.hboxlayout_widget([self.PDF_PERM_ACCESSIBILITY, self.PDF_PERM_PRINT_HQ])
        w6 = self.hboxlayout_widget([self.PDF_PERM_FORM, self.PDF_PERM_ANNOTATE])

        self.setLayout( QtWidgets.QVBoxLayout() )
        self.layout().addWidget(w0)
        self.layout().addWidget(w1)
        self.layout().addWidget(w2)
        self.layout().addWidget(w3)
        self.layout().addWidget(QtWidgets.QLabel('暗号化時にチェックを入れた項目を許可'))
        self.layout().addWidget(w4)
        self.layout().addWidget(w5)
        self.layout().addWidget(w6)
        
        self.owner_pw_generate.clicked.connect(self.owner_pw_generate_clicked)
        self.user_pw_generate.clicked.connect(self.user_pw_generate_clicked)
        self.open_folder.clicked.connect(self.open_folder_clicked)

    def open_folder_clicked(self):
        subprocess.run(['explorer', str(Path(self.folder.text()))])

    def hboxlayout_widget(self, widgets):
        w = QtWidgets.QWidget()
        w.setLayout( QtWidgets.QHBoxLayout() )
        w.layout().setContentsMargins(0, 0, 0, 0)
        for widget in widgets:
            w.layout().addWidget(widget)
        return w

    def encryption(self):
        if not self.user_pw_enable.isChecked() and not self.owner_pw_enable.isChecked():
            return fitz.PDF_ENCRYPT_NONE
        if self.encryption_text.currentText() == '読み込んだPDFの暗号化を保持':
            return fitz.PDF_ENCRYPT_KEEP
        if self.encryption_text.currentText() == '暗号化無し':
            return fitz.PDF_ENCRYPT_NONE
        if self.encryption_text.currentText() == '40 bit RC4':
            return fitz.PDF_ENCRYPT_RC4_40
        if self.encryption_text.currentText() == '128 bit RC4':
            return fitz.PDF_ENCRYPT_RC4_128
        if self.encryption_text.currentText() == '128 bit AES':
            return fitz.PDF_ENCRYPT_AES_128
        if self.encryption_text.currentText() == '256 bit AES':
            return fitz.PDF_ENCRYPT_AES_256
        return None

    def to_dict(self) -> dict:
        output = {}
        output['folder'] = self.folder.text()
        output['owner'] = {'pw':self.owner_pw_edit.text(), 'enable':self.owner_pw_enable.isChecked()}
        output['user'] = {'pw':self.owner_pw_edit.text(), 'enable':self.owner_pw_enable.isChecked()}
        output['encryption'] = self.encryption_text.currentText()
        output['PDF_PERM_ACCESSIBILITY'] = self.PDF_PERM_ACCESSIBILITY.isChecked()
        output['PDF_PERM_ANNOTATE'] = self.PDF_PERM_ANNOTATE.isChecked()
        output['PDF_PERM_ASSEMBLE'] = self.PDF_PERM_ASSEMBLE.isChecked()
        output['PDF_PERM_COPY'] = self.PDF_PERM_COPY.isChecked()
        output['PDF_PERM_FORM'] = self.PDF_PERM_FORM.isChecked()
        output['PDF_PERM_MODIFY'] = self.PDF_PERM_MODIFY.isChecked()
        output['PDF_PERM_PRINT'] = self.PDF_PERM_PRINT.isChecked()
        output['PDF_PERM_PRINT_HQ'] = self.PDF_PERM_PRINT_HQ.isChecked()
        return output

    def from_dict(self, data: dict):
        self.folder.setText(data['folder'])
        self.owner_pw_edit.setText(data['owner']['pw'])
        self.owner_pw_enable.setChecked(data['owner']['enable'])
        self.owner_pw_edit.setText(data['user']['pw'])
        self.owner_pw_enable.setChecked(data['user']['enable'])
        self.encryption_text.setCurrentText(data['encryption'])
        self.PDF_PERM_ACCESSIBILITY.setChecked(data['PDF_PERM_ACCESSIBILITY'])
        self.PDF_PERM_ANNOTATE.setChecked(data['PDF_PERM_ANNOTATE'])
        self.PDF_PERM_ASSEMBLE.setChecked(data['PDF_PERM_ASSEMBLE'])
        self.PDF_PERM_COPY.setChecked(data['PDF_PERM_COPY'])
        self.PDF_PERM_FORM.setChecked(data['PDF_PERM_FORM'])
        self.PDF_PERM_MODIFY.setChecked(data['PDF_PERM_MODIFY'])
        self.PDF_PERM_PRINT.setChecked(data['PDF_PERM_PRINT'])
        self.PDF_PERM_PRINT_HQ.setChecked(data['PDF_PERM_PRINT_HQ'])

    def user_pw_generate_clicked(self):
        self.user_pw_edit.setText( ''.join(random.choices(string.ascii_letters + string.digits, k=8)) )
        
    def owner_pw_generate_clicked(self):
        self.owner_pw_edit.setText( ''.join(random.choices(string.ascii_letters + string.digits, k=8)) )

    def permissions(self):
        permissions = -1
        if self.owner_pw_enable.isChecked() or self.user_pw_enable.isChecked():
            permissions  = fitz.PDF_PERM_ACCESSIBILITY if self.PDF_PERM_ACCESSIBILITY.isChecked() else 0
            permissions += fitz.PDF_PERM_ANNOTATE if self.PDF_PERM_ANNOTATE.isChecked() else 0
            permissions += fitz.PDF_PERM_ASSEMBLE if self.PDF_PERM_ASSEMBLE.isChecked() else 0
            permissions += fitz.PDF_PERM_COPY if self.PDF_PERM_COPY.isChecked() else 0
            permissions += fitz.PDF_PERM_FORM if self.PDF_PERM_FORM.isChecked() else 0
            permissions += fitz.PDF_PERM_MODIFY if self.PDF_PERM_MODIFY.isChecked() else 0
            permissions += fitz.PDF_PERM_PRINT if self.PDF_PERM_PRINT.isChecked() else 0
            permissions += fitz.PDF_PERM_PRINT_HQ if self.PDF_PERM_PRINT_HQ.isChecked() else 0
        return permissions

    def user_pw(self):
        if self.user_pw_enable.isChecked():
            return self.user_pw_edit.text()
        else:
            return None

    def owner_pw(self):
        if self.owner_pw_enable.isChecked():
            return self.owner_pw_edit.text()
        else:
            return None

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = OutputSettings()
    window.show()
    app.exec()

Share post

Related Posts

コメント