Moyu Office Automation~~PDF file merger, use Python to merge multiple PDF files

Instructions

Select multiple PDF files, and a new PDF file will be generated after the merge is performed. This new PDF file contains all the pages of the source PDF files.

Effect page display

insert image description here

There are a lot of resources that can be prostituted for nothing, and I will update the little knowledge of Python from time to time! !
Python source code, question answering, learning and exchange

Implementation process

Import the relevant third-party modules into the code block...

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
import os
import PyPDF2  # PDF操作库

QThread is a sub-thread application of PyQt5, which has been used many times before. In general use, create a class to handle thread-related logic. Note that QThread is inherited from this class, and the function application paradigm inside will basically not change after creation. An __init__ function is used for initialization, 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 sub-thread execution is completed. In this way, the main thread knows the execution status 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 + '/汇总.pdf')

        self.finished.emit(True)

Write the UI interface. There are relatively few UI components on the interface. The source file button is used to select the PDF files to be merged (multiple selection is supported, and multiple files can be selected by holding down the Ctrl key when selecting 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文件合并器')
        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('源文件')
        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('目标路径')
        self.pdf_tar_btn.clicked.connect(self.pdf_tar_btn_click)

        self.start_btn = QPushButton()
        self.start_btn.setText('开始合并吧')
        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(), '打开文件', '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(), '打开文件夹')
        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)
通过main函数启动应用...

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

Copy all the above code blocks into a .py python file, and start it directly to run.

Well, that’s the end of today’s case implementation. It’s a very simple case, but it’s very practical. If you have any questions, you can directly contact me for consultation.

Friends who want to learn Python can ↓ ↓ ↓

Click here~~

There are a lot of resources that can be prostituted for nothing, and I will update the little knowledge of Python from time to time! !

Guess you like

Origin blog.csdn.net/weixin_72934044/article/details/128135946