python GUI文档提取

摘自链接:https://blog.youkuaiyun.com/m0_48405781/article/details/111564739

import PySimpleGUI as sg
sg.ChangeLookAndFeel('GreenTan')   #更换主题
menu_def = [['&使用说明', ['&注意']]]
layout = [
    [sg.Menu(menu_def, tearoff=True)],
    [sg.Frame(layout=[
    [sg.Radio('Excel1', "RADIO1",size=(10,1),key="Excel1"),  sg.Radio('Word', "RADIO1",default=True,key="Word")],
    [sg.Radio('Excel2', "RADIO1", enable_events=True, size=(10,1),key="Excel2"), sg.Radio('PDF', "RADIO1",key="PDF")]], title='选项',title_color='red', relief=sg.RELIEF_SUNKEN, tooltip='Use these to set flags')],
    [sg.Text('文件位置', size=(8, 1), auto_size_text=False, justification='right'),
     sg.InputText(enable_events=True,key="lujing"), sg.FolderBrowse()],
    [sg.Text('文件名字', size=(8, 1), justification='right'),
     sg.InputText(enable_events=True,key="wenjian")],
    [sg.Submit(tooltip='文件'), sg.Cancel()]]

window = sg.Window('图片提取器', layout, default_element_size=(40, 1), grab_anywhere=False)
while True:
    event, values = window.read()
    if event == "Submit":
        if values["Excel2"] == True:
            '''
            事件绑定
            '''
            sg.Popup("提取成功")
        if values["Excel1"] == True:
            '''
            事件绑定
            '''
            sg.Popup("提取成功")
        if values["Word"] == True:
            '''
            事件绑定
            '''
            sg.Popup("提取成功") 
        if values["PDF"] == True:
            '''
            事件绑定
            '''
            sg.Popup("提取成功")
    if event == "Cancel" or event == sg.WIN_CLOSED:
        break    
    if event == "注意":
        sg.Popup("作用讲解:",
                 "Excel1 :解析选定位置中所有的Excel文件,无需在文件名处填写",
                 "Excel2 :解析选定位置中单个指定的Excel文件,需在文件名处填写",
                 "Word :  解析选定位置中单个指定的docx结尾的文件,无需在文件名处填写",
                 "PDF :   解析选定位置中单个指定的PDF文件,需在文件名处填写")
window.close()

后记:因为复制原链接代码的时候,报错,所以为了以后使用不报错而转载,没其他意思。

批量修改文件名(摘自:链接太长了

import os
import re
import time

"""对指定目录下的所有文件进行有选择的修改名称"""
def ReFileName(dirPath,pattern):
    """
    :param dirPath: 文件夹路径
    :param pattern: 正则匹配模式
    :return:
    """
    # 对目录下的文件进行遍历
    for file in os.listdir(dirPath):
        # 判断是否是文件
        if os.path.isfile(os.path.join(dirPath, file)) == True:
            # 用正则匹配,去掉不需要的词
            newName = re.sub(pattern, "", file)
            # 设置新文件名
            newFilename = file.replace(file, newName)
            # 重命名
            os.rename(os.path.join(dirPath, file), os.path.join(dirPath, newFilename))
    print("文件名已统一修改成功")


if __name__ == '__main__':
    timeStart = time.time()
    dirPath = r"C:\Users\Administrator\Desktop\test\sr"
    pattern = re.compile(r'要删除的字符串')
    ReFileName(dirPath,pattern)
    timeEnd = time.time()
    print("程序走了%d秒"%(timeEnd-timeStart))

最后将两者简单结合一下:

import PySimpleGUI as sg
import os
import re
def Less(dirPath,pattern):
    """
    :param dirPath: 文件夹路径
    :param pattern: 正则匹配模式
    :return:
    """
    # 对目录下的文件进行遍历
    for file in os.listdir(dirPath):
        # 判断是否是文件
        if os.path.isfile(os.path.join(dirPath, file)) == True:
            # 用正则匹配,去掉不需要的词
            newName = re.sub(pattern, "", file)
            # 设置新文件名
            newFilename = file.replace(file, newName)
            # 重命名
            os.rename(os.path.join(dirPath, file), os.path.join(dirPath, newFilename))
    print("文件名已统一修改成功")
def More(dirPath,pattern):
    """
    :param dirPath: 文件夹路径
    :param pattern: 正则匹配模式
    :return:
    """
    # 对目录下的文件进行遍历
    for file in os.listdir(dirPath):
        # 判断是否是文件
        if os.path.isfile(os.path.join(dirPath, file)) == True:
            # 用正则匹配,去掉不需要的词
            newName = str(pattern)+file
            # 设置新文件名
            newFilename = file.replace(file, newName)
            # 重命名
            os.rename(os.path.join(dirPath, file), os.path.join(dirPath, newFilename))
    print("文件名已统一修改成功")

sg.ChangeLookAndFeel('GreenTan')   #更换主题
menu_def = [['&使用说明', ['&注意']]]
layout = [
    [sg.Menu(menu_def, tearoff=True)],
    [sg.Text('文件夹位置', size=(8, 1), auto_size_text=False, justification='right'),
     sg.InputText(enable_events=True,key="pwd"), sg.FolderBrowse()],
    [sg.Text('删除', size=(8, 1), justification='right'),
     sg.InputText(enable_events=True,key="delStr")],
    [sg.Text('增添', size=(8, 1), justification="right"),
     sg.InputText(enable_events=True,key="addStr")],
    [sg.Submit(tooltip='文件'), sg.Cancel()]]
window = sg.Window('文件名批量修改', layout, default_element_size=(40, 1), grab_anywhere=False)
while True:
    event, values = window.read()
    if event == "Submit":
        pwd=values["pwd"]
        addStr=values["addStr"]
        delStr=re.compile(str(values["delStr"]))
        Less(pwd,delStr)
        More(pwd,addStr)
    if event == "Cancel" or event == sg.WIN_CLOSED:
        break    
    if event == "注意":
        sg.Popup("作用讲解:",
                 "增添 :增添为前缀",
                 "删除 :以正则方式删除",
                 "先删除,后增添"
                )
window.close()

最后将该文件打包成.exe文件:
pyinstaller -F -w .\文件名批量修改.py
(需要先安装pyinstaller)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值