Remove specific text from PDF file names using Python

When processing a large number of PDF files, sometimes we may need to batch modify file names. For example, we may need to remove or replace specific text in a file name. Today, I will show you how to use Python to write a simple program to select a folder and delete specified text in the file name.
C:\pythoncode\new\renamepdffilenname.py
Insert image description here

Preparation

First, we need to install the wxPython module, which is a Python module for creating desktop applications. You can use the pip tool to install it:

pip install wxPython

After the installation is complete, we can start writing our program.

Programming

We will use Python and the wxPython module to create a simple GUI program. The main function of the program is to select a folder, traverse all PDF files in the folder, and delete the specified text in the file name.

import os
import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title)

        panel = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)

        choose_btn = wx.Button(panel, label='选择文件夹')
        choose_btn.Bind(wx.EVT_BUTTON, self.on_choose_folder)
        vbox.Add(choose_btn, proportion=0, flag=wx.ALL|wx.CENTER, border=10)

        self.log_text = wx.TextCtrl(panel, style=wx.TE_MULTILINE|wx.TE_READONLY)
        vbox.Add(self.log_text, proportion=1, flag=wx.EXPAND|wx.ALL, border=10)

        panel.SetSizer(vbox)
        self.Show()

    def on_choose_folder(self, event):
        dialog = wx.DirDialog(self, "选择文件夹", style=wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)

        if dialog.ShowModal() == wx.ID_OK:
            folder_path = dialog.GetPath()
            self.process_pdf_files(folder_path)

        dialog.Destroy()

    def process_pdf_files(self, folder_path):
        self.log_text.Clear()
        self.log_text.AppendText("处理中...\n")

        for root, dirs, files in os.walk(folder_path):
            for file in files:
                if file.lower().endswith(".pdf"):
                    file_path = os.path.join(root, file)
                    new_file_name = file.replace("-CSDN博客", "")
                    new_file_path = os.path.join(root, new_file_name)

                    try:
                        os.rename(file_path, new_file_path)
                        self.log_text.AppendText(f"重命名文件: {
      
      file_path} -> {
      
      new_file_path}\n")
                    except Exception as e:
                        self.log_text.AppendText(f"重命名文件时出错: {
      
      file_path}\n")
                        self.log_text.AppendText(f"错误信息: {
      
      str(e)}\n")

        self.log_text.AppendText("处理完成!")

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame(None, "PDF文件重命名")
    app.MainLoop()

Run program

Save the above code as a Python script file, and then run the script. The program will launch a GUI window and you can click on the button to select the folder to process.

After selecting the folder, the program will traverse all PDF files under the folder and delete the "-CSDN Blog" text in the file name. The processing results will be displayed in the log text box of the program window.

Summarize

By using Python and the wxPython module, we can easily create a GUI program for batch processing of PDF file names. This program can help us quickly delete specific text in file names and improve work efficiency.

おすすめ

転載: blog.csdn.net/winniezhang/article/details/133196721