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

Posted by Blob on Fri, 04 Mar 2022 06:01:44 +0100

Relevant documents

Little buddy who wants to learn Python can pay attention to the official account of Xiaobian [Python journal].
There are many resources for whoring for nothing, ha. I will update the little knowledge of Python from time to time!!
Python source code, problem solving, learning exchange group: 773162165

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.

Effect page display

Implementation process

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')
        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)
adopt main Function start application...

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.

Well, that's all for today's case implementation. It's a very simple small case, but it's very practical. If you have any questions, you can directly consult me.

Little buddy who wants to learn Python can pay attention to the official account of Xiaobian [Python journal].
There are many resources for whoring for nothing, ha. I will update the little knowledge of Python from time to time!!
Python source code, problem solving, learning exchange group: 773162165

Topics: Python Back-end