针对文件被更改后缀,用py写了个恢复文件原始的后缀的小工具!

头一回使用用py写带界面的工具,
献丑了各位!
还打包成 exe 了, 可以直接使用!

import os
import shutil
from tkinter import messagebox, filedialog, Frame, StringVar, Label, Entry, Button, Tk
import filetype


def count_w_h_x_y(main_root):
    # 获取当前屏幕的宽度和高度
    screen_width = main_root.winfo_screenwidth()
    screen_height = main_root.winfo_screenheight()

    _width = int(screen_width * 2.8 / 10)
    _height = int(screen_height * 3 / 10)
    _float_x = int((screen_width - _width) / 2)
    _float_y = int((screen_height - _height) / 2)

    return _width, _height, _float_x, _float_y


class TkApplication(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.root_path = StringVar()
        self.open_path = ''
        self.master = master
        self.create_widget()

    def create_widget(self):
        """
        原文件支持类型:
        image:jpg,png,gif,web,cr2,tif,bmp,jxr,psd,ico
        video:mp4,m4v,mkv,web,mov,avi,wmv,mpg,flv
        audio:mid,mp3,m4a,ogg,flac,wav,amr
        application: epub,zip,tar,rar,gz,bz2,7z,xz,pdf,exe,swf,rtf,eot,ps,sqlite,nes,crx,cab,deb,ar,Z,lz
        font: woff,woff2,ttf,otf
        """
        Label(root, text='选择文件夹: ', font=('华文彩云', 15)).place(x=50, y=80, height=30)
        Entry(root, textvariable=self.root_path, font=('FangSong', 10), width=30, state='readonly').place(x=170, y=80,
                                                                                                          height=30)
        Button(root, text='选择路径', command=self.get_path).place(x=400, y=80, height=30)
        Button(root, text='确定(开始执行)', font=('华文彩云', 15), command=self.deal_fix_img).place(x=50, y=150, width=410)

    def get_path(self):
        # 返回一个字符串,可以获取到任意文件的路径。
        path1 = filedialog.askdirectory(title='请选择文件')
        self.root_path.set(path1)

    def deal_fix_img(self):
        # dir_str = r'C:UsersAdministratorPicturesfix_img'
        dir_str = self.root_path.get()
        if not dir_str:
            # 请选择路径
            messagebox.showwarning('警告!', '请选择路径!')
            return

        fix_success_dir = f"{dir_str}/fix_success_dir"
        if not os.path.exists(fix_success_dir):
            os.makedirs(fix_success_dir)

        if not os.listdir(dir_str):
            messagebox.showwarning('警告!', '所选文件夹下无文件!')
            return
        for file in os.listdir(dir_str):
            abs_file_path = f"{dir_str}/{file}"
            if os.path.isdir(abs_file_path):
                continue
            try:
                kind = filetype.guess(abs_file_path)
                if kind is None:
                    continue
                file_type = kind.extension
            except Exception as e:
                continue
            old_file_name, old_file_type = os.path.splitext(file)
            if old_file_type.lower() == f'.{file_type}' or not file_type:
                continue

            new_img = f"{fix_success_dir}/{old_file_name}.{file_type}"
            shutil.copy2(abs_file_path, new_img)

        messagebox.showinfo('OK!', '处理完成')
        self.open_path = fix_success_dir

        Button(root, text='打开处理文件夹!', font=('华文彩云', 15), command=self.open).place(x=50, y=190, width=410)

    def open(self):
        os.system(f"start {self.open_path}")


if __name__ == '__main__':
    root = Tk()
    root.title('批量修复文件原始格式!')
    width, height, float_x, float_y = count_w_h_x_y(root)
    root.geometry(f'{width}x{height}+{float_x}+{float_y}')  # 设置窗口大小 左上角X,Y坐标
    app = TkApplication(master=root)

    root.mainloop()

度盘: 链接:https://pan.baidu.com/s/1lBnPjkZxEU1R70riBrQQmg?pwd=l8sm
提取码:l8sm
–来自百度网盘超级会员V8的分享

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 共3条

请登录后发表评论