美文网首页
python 文件内容搜索器

python 文件内容搜索器

作者: 火卫控 | 来源:发表于2023-05-16 09:59 被阅读0次

    python 文件内容搜索器

    在指定文件夹下(包括子文件)搜索文件内文本

    txt .docx .pdf .ppt .excel等

    用到search4file包

    示例如下:


    搜索文件内容示例

    代码如下:

    from tkinter import *
    
    import json
    
    
    import ctypes
    whnd = ctypes.windll.kernel32.GetConsoleWindow()
    if whnd != 0:
        ctypes.windll.user32.ShowWindow(whnd, 0)
        ctypes.windll.kernel32.CloseHandle(whnd)
    
        
     
    tk = Tk()
    tk.title('文件内容搜索器v1.0')
    
    # tk.geometry('1000x800')
    
    #获取屏幕尺寸计算参数,使窗口显示再屏幕中央
    
    screen_width = tk.winfo_screenwidth() 
    screen_height = tk.winfo_screenheight()
    width = 1400
    height = 900
    # window_size = '%dx%d+%d+%d' % (width, height, (screen_width-width)/2, (screen_height-height)/2)
    tk_size = f'{width}x{height}+{round((screen_width-width)/2)}+{round((screen_height-height)/2)}' #round去掉小数
    tk.geometry(tk_size)  
    
    def clear2():
        
        now_bok1.delete(0, "end")
        now_bok1.insert(0, "请输入地址: 如 D:/docu")
    
    def clear3():
       
        now_bok2.delete(0, "end")
        now_bok2.insert(INSERT, "请输入查找文本: 如 virus")
    
    
    
    
    #  输入框1
    now_nub1 = Label(tk, font=('宋体', '20'),text='1、请输入地址:')
    now_nub1.grid(row=1, column=1, sticky='W')
    now_bok1 = Entry(tk, font=('宋体', '18'), width=50)
    now_bok1.grid(row=1, column=2, sticky='NW')
    clearbutton = Button(tk,text='重新输入地址',command=clear2)
    clearbutton.grid(row=1, column=3, sticky='NW')
    
    #  输入框2
    now_nub2 = Label(tk, font=('宋体', '20'), text='2、请输入搜索内容:')
    now_nub2.grid(row=2, column=1, sticky='E')
    now_bok2 = Entry(tk, font=('宋体', '20'), width=50)
    now_bok2.grid(row=2, column=2, sticky='SW')
    clearbutton = Button(tk,text='重新输入需搜索内容',command=clear3)
    clearbutton.grid(row=2, column=3, sticky='SW')
    
    
    
    #  输出结果
    Output_results = Label(tk, font=('Helvetica', '20'), text='输出结果:')
    Output_results.grid(row=8, column=1, sticky='NW')
    
    result_data_Text = Text(tk, width=110, height=36,font=('Helvetica', '12'))  #处理结果展示
    result_data_Text.grid(row=10, column=2, rowspan=15, columnspan=10, sticky='NW')
     
    a = Message(tk,text = 'hello Message')
    a.grid(row=5, column=3, rowspan=15, columnspan=10, sticky='NW')
    
    
    def btnClick():
        # Message.info("消息", "你好呀~")
        import tkinter.messagebox
        tkinter.messagebox.showinfo("Help Info.",  "版本号: \t1.0\n更新日期:\t2023.5.16\n作者:\t艾强云\n联系方式:\t1154282938@qq.com\n\nWangLab\nGZlab\t广州实验室 ")
    
    
    # 创建按钮
    btn_info = Button(tk, font=('Helvetica', '20'),text="帮助", command=btnClick)
    btn_info.grid(row=5, column=5, rowspan=15, columnspan=10, sticky='NW')
    
    
    path = now_bok1.get()
    content = now_bok2.get()
    
    
    import os,sys
    def restart():
        
        os.execl(sys.executable, sys.executable, *sys.argv)
        # now_bok1.insert(INSERT, " 以下是新查询结果: \n")
    
    
    
    def clear():
        result_data_Text.delete(1.0, "end")
        result_data_Text.insert(END, " 按下 --查询-- 按钮,获取累积查询结果: (如果想初始化查询,请点击 --RESTART-- 按钮)\n")
    
    clearbutton = Button(tk,font=('Helvetica', '20'),text='Clear',command=clear)
    clearbutton.grid(row=5, column=3, sticky='SW')
    
    restartbutton = Button(tk,font=('Helvetica', '20'),text='RESTART',command=restart)
    restartbutton.grid(row=5, column=4, sticky='SW')
     
    
    def value():
        path = now_bok1.get()
        content = now_bok2.get()
        import search4file
        p = search4file.search_by_content(search_path=path,search_content=content)
        r = json.loads(p)
        txt = r['search_result']
        result = r['search_result']
        print(result)
                # result2  = json.dumps(result)
                # print(result2)
        result_show = []
        
    
        for v,k in result.items():
                # print(k)
            for v2,k2 in k.items():   
                print(k2)
                result_show.append(k2)
                
    
        res = []
        str_show = ''
        [res.append(i) for i in result_show if i not in res]  #去重
    
        for i in res:
            str_show = str_show + i +'\n\n'
    
        result_data_Text.insert(INSERT, str_show)
    
        # result_data_Text.grid(row=10, column=2, rowspan=15, columnspan=10)
     
     
    AnNiu = Button(tk, font=('Helvetica', '20'),text='查询', fg='blue', bd=2, width=10, command=value)
    AnNiu.grid(row=5, column=2, sticky='NW')
     
     
    tk.mainloop()
    

    相关文章

      网友评论

          本文标题:python 文件内容搜索器

          本文链接:https://www.haomeiwen.com/subject/baovsdtx.html