Office automation: PDF file combiner, which combines multiple PDF files

Posted by ecaandrew on Wed, 02 Mar 2022 14:26:33 +0100

Operating instructions: select multiple PDF files, and a new PDF file will be generated after merging. This new PDF file contains the pages of all source PDF files.

[read the full text]

Import relevant third-party modules into code blocks

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
import os
import PyPDF2  # PDF operation Library

QThread is a sub thread application of PyQt5, which has been used more times before. In general, you can create a class to deal with thread related logic. Note that QThread is inherited from this class. After it is created, the function application paradigm will not change. One__ init__ Function is used to initialize, another del function controls the destruction of the thread, and a run function is used to write the business logic in the thread.

This defines a semaphore finished

finished = pyqtSignal(bool)

This variable is mainly used to transfer the variable value to the main thread when the execution of the child thread is completed. In this way, the main thread knows the execution state of the child thread.

class WorkThread(QThread):
    finished = pyqtSignal(bool)

    def __init__(self, parent=None):
        super(WorkThread, self).__init__(parent)
        self.parent = parent
        self.working = True

    def __del__(self):
        self.working = False
        self.wait()

    def run(self):
        pdf_files_path = self.parent.pdf_files_path.text().strip()
        pdf_tar_dir = self.parent.pdf_tar_dir.text().strip()

        file_list = pdf_files_path.split(',')

        merge = PyPDF2.PdfFileMerger()
        for file in file_list:
            merge.append(PyPDF2.PdfFileReader(file))
        merge.write(pdf_tar_dir + '/Summary.pdf')

        self.finished.emit(True)

Write UI interface. There are few UI components on the interface. The source file button is to select PDF files to be merged (multiple selection is supported. When selecting files, press and hold Ctrl to select multiple files). The target path is to select the path to store the generated merged file. After selecting, click the start button to call the sub thread to perform the PDF file merging operation.

class PDFMerge(QWidget):
    def __init__(self):
        super(PDFMerge, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('PDF File merge device official account:[Python concentration camp]')
        self.setWindowIcon(QIcon('pdf.ico'))
        self.setFixedWidth(500)
        self.setFixedHeight(120)

        grid = QGridLayout()

        self.pdf_files_path = QLineEdit()
        self.pdf_files_path.setReadOnly(True)

        self.pdf_files_btn = QPushButton()
        self.pdf_files_btn.setText('source file')
        self.pdf_files_btn.clicked.connect(self.pdf_files_btn_click)

        self.pdf_tar_dir = QLineEdit()
        self.pdf_tar_dir.setReadOnly(True)

        self.pdf_tar_btn = QPushButton()
        self.pdf_tar_btn.setText('Target path')
        self.pdf_tar_btn.clicked.connect(self.pdf_tar_btn_click)

        self.start_btn = QPushButton()
        self.start_btn.setText('Start merging')
        self.start_btn.clicked.connect(self.start_btn_click)

        grid.addWidget(self.pdf_files_path, 0, 0, 1, 1)
        grid.addWidget(self.pdf_files_btn, 0, 1, 1, 1)

        grid.addWidget(self.pdf_tar_dir, 1, 0, 1, 1)
        grid.addWidget(self.pdf_tar_btn, 1, 1, 1, 1)

        grid.addWidget(self.start_btn, 2, 0, 1, 2)

        self.thread_ = WorkThread(self)
        self.thread_.finished.connect(self.finished)

        self.setLayout(grid)

    def pdf_files_btn_click(self):
        files = QFileDialog.getOpenFileNames(self, os.getcwd(), 'Open file', 'PDF Files(*.pdf)')
        file_list = files[0]
        self.pdf_files_path.setText(','.join(file_list))

    def pdf_tar_btn_click(self):
        dir = QFileDialog.getExistingDirectory(self, os.getcwd(), 'open a folder')
        self.pdf_tar_dir.setText(dir)

    def start_btn_click(self):
        self.start_btn.setEnabled(False)
        self.thread_.start()

    def finished(self, finished):
        if finished is True:
            self.start_btn.setEnabled(True)

Start the application through the main function

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = PDFMerge()
    main.show()
    sys.exit(app.exec_())

copy all the above code blocks to one In the python file of py, you can start it directly and run it.

If you have any questions, please leave a message. Bloggers must know everything and say everything!

[previous highlights]

GUI guess number game, play directly

Teach you to make a data chart generator (with source code)

Dynamic pointer clock: use pyqt5 to make pointer clock to display real-time time time

hashlib.md5() function to filter out system duplicate files and remove

The brightest kid in python log is the fancy acridine

Hassa, here comes the hero League full skin downloader

Freehand picture generator Shuey Rhon Rhon as an example, a key to generate.

PyQt5 sensitive word detection tool production, operator's Gospel

Bing dwen dwen, the mascot just released, is attached to the source.

The most beautiful form viewing plug-in: tabulate

Freehand picture generator Shuey Rhon Rhon as an example, a key to generate.

On the second day of the lunar new year, I made a windows notification manager!

Topics: Python