美文网首页
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仿evething的文件搜索器 !

    今天看到everything搜索速度秒杀windows自带的文件管理器,所以特地模仿everything实现了文件...

  • python复制100遍txt文件内容

    python复制100遍txt文件内容

  • python处理文件内容

    1. Python定义变量来命名文件的名称,案例方法代码如下: //以str1变量命名文件名 //错误方法:不能用...

  • Python文件内容替换

    任务:将pep.muscle.fasta.treefile文件中的信息 根据id.list列表内容进行替换,得到r...

  • Python 文件内容读取

    背景 计划实现文件中心,支撑检索常见文件内容 依赖包 基础文件读取 .txt .xls .xlsx .docx ....

  • Python读取文件内容

    read(),一次读取全部内容到内存。 with方式可以避免没有关闭资源文件产生错误 readlines(),wi...

  • python爬取当当网图书信息

    使用Python提取本地HTML文件中的内容 获取HTML文件中的内容 读取这个HTML文件,然后获得这个文件所...

  • 2020-11-13

    python 整合所有 txt 文件内容 需求: 将文件夹中的所有 txt 文件内容给整合到一个文件中去 难点: ...

  • python中的文件操作总结,你是否已经掌握通透?

    python的意思是用外语打招呼 目录 文件:数据的抽象和集合 文件的打开关闭 文件内容的读取 学习Python中...

  • 文件操作

    Python基础教程 文件内容操作 python中的文件操作还是挺简单的,类似于php中的文件方法,精简好用。我们...

网友评论

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

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