A PDF merge and split program using the PyMuPDF library

The PDF Tools application is a simple tool written using wxPython and the PyMuPDF library to merge and split PDF files. It provides a user-friendly graphical interface that allows users to select source and destination folders and perform operations on PDF files.
C:\pythoncode\blog\pdfmergandsplit.py
insert image description here

Features

  • Select Folder : Users can easily select source and destination folders through the application interface.
  • Merge PDF files : The application allows users to select PDF files to merge and merge them into a single PDF file.
  • Split PDF files : Users can select a PDF file to split it into multiple separate PDF files, each containing one page.

program implementation

The application uses the following libraries and modules:

  • wxPython : Graphical user interface for creating applications.
  • PyMuPDF : A library for manipulating PDF files.

The main part of the application is the wx.Framemain window, a class inherited from wxPython. A window contains the following components:

  • Select the buttons for the source and destination folders.
  • PDF file list box, displaying the PDF files in the source folder.
  • Merge and Split buttons for performing corresponding actions.

When the user taps the Select Source Folder button, the application displays a folder selection dialog where the user can select the source folder. After selection, the application gets the folder path and lists the PDF files in the folder. The Merge and Split buttons then become available and the user can perform the appropriate action.

The click event of the merge button will pop up a text input dialog box, requiring the user to enter the name of the merged file. After user input, the application calls the PyMuPDF library to merge the selected PDF files, and saves the merged PDF files to the target folder.

The click event of the split button iterates through the selected PDF files and saves each page as a separate PDF file using the PyMuPDF library.

code example

Here is a code sample for the application:

import os
import wx
import fitz

class PDFToolApp(wx.Frame):
    def __init__(self, parent, title):
        super(PDFToolApp, self).__init__(parent, title=title, size=(400, 400))

        self.panel = wx.Panel(self)
        self.source_folder_btn = wx.Button(self.panel, -1, "选择源文件夹")
        self.target_folder_btn = wx.Button(self.panel, -1, "选择目标文件夹")
        self.pdf_list = wx.CheckListBox(self.panel, -1, choices=[], style=wx.LB_MULTIPLE)
        self.merge_btn = wx.Button(self.panel, -1, "合并")
        self.split_btn = wx.Button(self.panel, -1, "分拆")
        self.merge_btn.Disable()
        self.split_btn.Disable()

        self.source_folder_btn.Bind(wx.EVT_BUTTON, self.on_select_source_folder)
        self.target_folder_btn.Bind(wx.EVT_BUTTON, self.on_select_target_folder)
        self.merge_btn.Bind(wx.EVT_BUTTON, self.on_merge)
        self.split_btn.Bind(wx.EVT_BUTTON, self.on_split)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.source_folder_btn, 0, wx.ALIGN_CENTER | wx.ALL, 10)
        sizer.Add(self.target_folder_btn, 0, wx.ALIGN_CENTER | wx.ALL, 10)
        sizer.Add(self.pdf_list, 1, wx.EXPAND | wx.ALL, 10)
        sizer.Add(self.merge_btn, 0, wx.ALIGN_CENTER | wx.ALL, 10)
        sizer.Add(self.split_btn, 0, wx.ALIGN_CENTER | wx.ALL, 10)
        self.panel.SetSizer(sizer)

        self.Show()

    def on_select_source_folder(self, event):
        dlg = wx.DirDialog(self, "选择源文件夹", style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            source_folder_path = dlg.GetPath()
            self.pdf_list.Set(self.get_pdf_files(source_folder_path))
            self.merge_btn.Enable()
            self.split_btn.Enable()
        dlg.Destroy()

    def on_select_target_folder(self, event):
        dlg = wx.DirDialog(self, "选择目标文件夹", style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            self.target_folder_path = dlg.GetPath()
        dlg.Destroy()

    def on_merge(self, event):
        selected_items = self.pdf_list.GetCheckedItems()
        if len(selected_items) > 0:
            dlg = wx.TextEntryDialog(self, "请输入合并后的文件名(不带扩展名):", "合并文件")
            if dlg.ShowModal() == wx.ID_OK:
                output_filename = dlg.GetValue()
                output_filepath = os.path.join(self.target_folder_path, output_filename + ".pdf")
                self.merge_pdfs(selected_items, output_filepath)
                wx.MessageBox("PDF文件合并完成!", "完成", wx.OK | wx.ICON_INFORMATION)
            dlg.Destroy()

    def on_split(self, event):
        selected_items = self.pdf_list.GetCheckedItems()
        if len(selected_items) > 0:
            for index in selected_items:
                pdf_filename = self.pdf_list.GetString(index)
                pdf_filepath = os.path.join(self.target_folder_path, pdf_filename)
                self.split_pdf(pdf_filename, pdf_filepath)
            wx.MessageBox("PDF文件分拆完成!", "完成", wx.OK | wx.ICON_INFORMATION)

    def merge_pdfs(self, selected_items, output_filepath):
        pdf_merger = fitz.open()

        for index in selected_items:
            pdf_filename = self.pdf_list.GetString(index)
            pdf_filepath = os.path.join(self.target_folder_path, pdf_filename)
            pdf = fitz.open(pdf_filepath)
            pdf_merger.insert_pdf(pdf)

        pdf_merger.save(output_filepath)
        pdf_merger.close()

    def split_pdf(self, pdf_filename, pdf_filepath):
        pdf = fitz.open(pdf_filepath)
        num_pages = pdf.page_count

        for i in range(num_pages):
            output_filename = f"{
      
      pdf_filename[:-4]}_{
      
      i+1}.pdf"
            output_filepath = os.path.join(self.target_folder_path, os.path.basename(output_filename) )
            print("output_filename:"+output_filename)
            page = pdf[i]
            new_pdf = fitz.open()
            new_pdf.insert_pdf(pdf, from_page=i, to_page=i)
            new_pdf.save(output_filepath)
            print("self.target_folder_path:"+self.target_folder_path)
            print("output_filepath:"+output_filepath)
            new_pdf.close()

        pdf.close()

    def get_pdf_files(self, folder_path):
        pdf_files = []
        for filename in os.listdir(folder_path):
            if filename.endswith(".pdf"):
                pdf_files.append(folder_path+'/'+filename)
        return pdf_files

if __name__ == "__main__":
    app = wx.App()
    PDFToolApp(None, "PDF工具")
    app.MainLoop()

Summarize

The PDF Tools application is a simple utility written using wxPython and the PyMuPDF library for merging and splitting PDF files. It provides an intuitive graphical interface that enables users to easily select folders and perform operations. Whether it is dealing with a large number of PDF files or a simple split operation, the application can meet the needs of users.

Guess you like

Origin blog.csdn.net/winniezhang/article/details/132333475