import os import sys import shutil import time import tkinter import threading from tkinter import * from tkinter import messagebox as ms from tkinter import Tk, Checkbutton, Label from tkinter import StringVar,IntVar from tkinter.filedialog import askdirectory from tkinter.filedialog import askopenfilenames import logging import unittest import stat #得到log位置 result_path = os.getcwd()+'/LOG/' selfwidth = 420 # 窗口x值 selfheight = 90 # 窗口y值 TIP_S = ["PDF↓状态对PDF权限还原","PDF解密后 为只读状态","会存在个别pdf解密失败,请选择文件重试一哈","选择文件时可以多选","请在脚本26行 屏蔽无需解密的文件后缀"] # 配置忽略需要的文件后缀 ignoeFileName = (".zip",".ini",".bat",".config",".sln",".rc",".exe",".dll",".lib",".bin",".rar") class lgtest(unittest.TestCase): if not os.path.exists(result_path): os.mkdir(result_path) if os.path.exists(result_path+'Ylog.log'): os.remove(result_path+'Ylog.log') logging.basicConfig(filename='LOG/'+"Ylog"+'.log', format='[%(asctime)s]-| %(message)s',#format='[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]', level=logging.DEBUG, filemode='w', datefmt='%Y-%m-%d%I:%M:%S %p') ''' Filename:指定路径的文件。这里使用了+—name—+是将log命名为当前py的文件名 Format:设置log的显示格式(即在文档中看到的格式)。分别是时间+当前文件名+log输出级别+输出的信息 Level:输出的log级别,优先级比设置的级别低的将不会被输出保存到log文档中 Filemode: log打开模式 a:代表每次运行程序都继续写log。即不覆盖之前保存的log信息。 w:代表每次运行程序都重新写log。即覆盖之前保存的log信息 CRITICAL 50 ERROR 40 WARNING 30 INFO 20 DEBUG 10 NOTSET 0 ''' global v global sum_num global f_num global pdf_status global tips global error_num error_num = 0 class myThread_dir(threading.Thread): def __init__(self, threadID, al_path): threading.Thread.__init__(self) self.threadID = threadID self.al_path = al_path def run(self): global sum_num global f_num f_num = 0 sum_num = 0 sum_files(self.al_path) if sum_num == 0: v.set("未找到PDF文件") else: alter_dir(self.al_path) def sum_files(al_path): v.set("计算中...") for root, dirs, files in os.walk(al_path): for file in files: global sum_num if pdf_status.get(): if os.path.splitext(file)[1] == ".pdf" or os.path.splitext(file)[1] == ".pdf4": sum_num = sum_num+1 else: if os.path.splitext(file)[1] not in ignoeFileName: sum_num = sum_num + 1 def alter_dir(al_path): for root, dirs, files in os.walk(al_path): for file in files: global f_num if os.path.splitext(file)[1] not in ignoeFileName: f_num = f_num + 1 else: continue portion = os.path.splitext(file) # 分离文件名字和后缀 # newportion0 = portion[0].replace(" ","").strip() # 去除文件中的空格 encoding='utf-8' newportion0 =portion[0] # 不去除空格 在命令中加 "" 解决的问题 Rnewname = newportion0 + portion[1] # 要改的新后缀 if pdf_status.get(): if portion[1] == ".pdf": os.popen(r'echo Y|cacls "%s" /p everyone:F' % os.path.join(root,file)) f_num = f_num + 1 continue try: newname = newportion0 + ".ini" # 要改的新后缀 os.chdir(root) os.rename(file, newname) os.chdir(root) logging.info('"%s" "%s"' % (newname, Rnewname)) result = os.popen(r'ren "%s" "%s"' % (newname, Rnewname)) if portion[1] == ".pdf": os.popen(r'echo Y|cacls "%s" /p everyone:r' % (os.path.join(root,Rnewname))) #Timer(1, My_schedule).start() logging.info(result.read()) logging.info("文件:" + os.path.join(root,file)+" 转换完成") except FileExistsError: global error_num error_num += 1 logging.info("error:---------------" + '"%s"文件已存在' % os.path.join(root,newname) + "---------------") #ms.showwarning("错误", "文件已存在,请查看log") except PermissionError: logging.info("error:---------------" + '"%s"文件被占用' % os.path.join(root,file) + "---------------") error_num += 1 except: logging.info("error:---------------" + '"%s"发生未知错误' % os.path.join(root,file) + "---------------") error_num += 1 def alter_file(al_path): for files in al_path: global f_num file = os.path.basename(files) if os.path.splitext(file)[1] not in ignoeFileName: f_num = f_num + 1 else: continue portion = os.path.splitext(file) # 分离文件名字和后缀 Rnewname = portion[0] + portion[1] # 要改的新后缀 if pdf_status.get(): if portion[1]== ".pdf": #print(r'echo Y|cacls "%s" /p everyone:F' % (os.path.join(os.path.dirname(files),newname))) os.popen(r'echo Y|cacls "%s" /p everyone:F' % (os.path.join(os.path.dirname(files),Rnewname))) f_num = f_num + 1 continue newname = portion[0] + ".ini" # 要改的新后缀 os.chdir(os.path.dirname(files)) try: os.rename(file, newname) if portion[1] == ".pdf": os.popen(r'echo Y|cacls "%s" /p everyone:r' % files) os.chdir(os.path.dirname(files)) result = os.popen(r'ren "%s" "%s"' % (newname, Rnewname)) # Timer(1, My_schedule).start() logging.info('"%s" "%s"' % (newname, Rnewname)) logging.info(result.read()) logging.info("文件00:" + file + " 转换完成") except FileExistsError: logging.info("error:---------------"+'"%s"文件已存在' % (os.path.dirname(files)+newname) +"---------------") global error_num error_num += 1 #ms.showwarning("错误","文件已存在,请查看log") except PermissionError: logging.info("error:---------------" + '"%s"文件被占用' % files + "---------------") error_num += 1 except: logging.info("error:---------------" + '"%s"发生未知错误' % files + "---------------") #print("Unexpected error:", sys.exc_info()[0]) error_num += 1 def select(): global selfwidth global selfheight root = tkinter.Tk(className='文件:') #创建GUI对象 screenwidth = root.winfo_screenwidth() #得到屏幕分辨率x screenheight = root.winfo_screenheight() #得到屏幕分辨率y x = (screenwidth - selfwidth) / 2 #得到软件在中心时 左上角的位置x y = (screenheight - selfheight) / 2 #得到软件在中心时 左上角的位置y root.geometry("%dx%d+%d+%d" % (selfwidth, selfheight, x, y - 100)) # 设置弹出框的大小 w x h path = StringVar() def selectPath(): path_ = askdirectory() path.set(path_) if path_ != "": #方法一 thread1 = myThread_dir(1,path_) thread1.setDaemon(True) #守护主线程 thread1.start() # 方法二 #threading.Thread(target=alter_dir,args=(path_,),daemon=True).start() def selsectFiles(): global sum_num global f_num sum_num,f_num = 0, 0 cur = askopenfilenames() if cur != "": v.set("计算中...") #文本显示选中的文件 path.set(cur) #得到文件个数 sum_num = len(cur) for file in cur: if os.path.splitext(file)[1] in ignoeFileName: sum_num = sum_num-1 if sum_num < 0: sum_num = 0 #得到PDF文件个数 if pdf_status.get(): sum_num = 0 for file in cur: if os.path.splitext(file)[1] == ".pdf" or os.path.splitext(file)[1] == ".pdf4": sum_num = sum_num+1 if sum_num == 0: v.set("未找到PDF文件") else: threading.Thread(target=alter_file, args=(cur,), daemon=True).start() def Pdf_Files(): if f_num != sum_num: pdf_status.set(1-pdf_status.get()) print("转换中禁止操作") else: if pdf_status.get(): check_Text.set("PDF↓") else: check_Text.set("PDF↑") global v, pdf_status, tips pdf_status, v, check_Text, tips = IntVar(), StringVar(), StringVar(), StringVar() tips.set(TIP_S[0]) pdf_status.set(0) check_Text.set("PDF↑") v.set("自用 勿传") Label(root, text=" 目 标 :").grid(row=0, column=0) Label(root, textvariable=v).grid(row=3, column=1) Label(root, textvariable=tips).grid(row=1, column=1) ent = Entry(root, textvariable=path,width=40).grid(row=0, column=1) bnt = Button(root, text="选文件夹",width=13, command=selectPath).grid(row=0, column=2) bnt1 = Button(root, text="多选文件", width=13, command=selsectFiles).grid(row=1, column=2) Checkbutton(root,variable=pdf_status, textvariable=check_Text,command =Pdf_Files,indicatoron=0).grid(row=1, column=0) root.mainloop() def My_schedule(): ''' :return: ''' mun_tip = 0 tip_flag = len(TIP_S) tip_flag0 = 0 while True: #总文件数 - 已转换文件数 = 秒数 转换时间 到一般格式 m, s = divmod(sum_num-f_num, 60) h, m = divmod(m, 60) mun_tip = mun_tip + 1 # 输出剩余时间 进度 if f_num != sum_num: v.set("进度:%d/%d 剩余时间:%d:%d:%d"%(f_num,sum_num,h,m,s)) #如果转换完成 即已转换文件数 == 总文件数 输出相应的信息 if sum_num!=0 and f_num == sum_num: time.sleep(1) if sum_num != 0 and f_num >= sum_num: v.set("转换完成") global error_num if error_num != 0: ms.showwarning("错误", "%d个错误,请查看log"%error_num) error_num = 0 #在线程开启2s后 循环打印提示信息 if 2 6: mun_tip = 2 tip_flag0 = tip_flag0+1 if tip_flag0 == tip_flag: tip_flag0 = 0 time.sleep(1) if __name__ == '__main__': f_num,sum_num = 0,0 threading.Thread(target=My_schedule,daemon=True).start() select()