批量搜索word内文字 您所在的位置:网站首页 excel查找关键字并标注 批量搜索word内文字

批量搜索word内文字

2023-03-09 13:52| 来源: 网络整理| 查看: 265

遇到这么一个情景,我有很多个word文档,因为日期过于久远,一时想不起来我想找的是哪个文档。但是我只能记得文档里面有一个李二狗的名字!

如果一个一个去搜素,那么太耗时哦!

于是此脚本诞生了。 帮到你了,请点个关注!谢谢!

 

还是一样,先看思路

一、导入需要用到的模块

 二、画界面

 

三、 搜索内容

 结束!

非常方便

整体代码如下

import os import tkinter as tk from tkinter import ttk from tkinter import filedialog from tkinter import messagebox # 创建窗口 window = tk.Tk() window.title('批量搜索Word文字') window.geometry('600x400') # 加载窗口 var_dir_path = tk.StringVar() var_search_word = tk.StringVar() # 搜索函数 def search_func(): if var_dir_path.get() == '': messagebox.showwarning(title='警告', message='请先选择搜索路径!') elif var_search_word.get() == '': messagebox.showwarning(title='警告', message='请输入要搜索的内容!') else: result_text.delete(1.0, tk.END) dir_path = var_dir_path.get() search_word = var_search_word.get() for root, dirs, files in os.walk(dir_path): for file in files: if os.path.splitext(file)[1] == '.docx': file_name = os.path.join(root, file) with open(file_name, 'r', encoding='utf-8') as f: content = f.read() if search_word in content: result_text.insert(tk.END, file_name + '\n') # 创建控件 # 标签 tk.Label(window, text='搜索路径:').place(x=20, y=20) tk.Label(window, text='搜索内容:').place(x=20, y=50) # 输入框 entry_dir_path = ttk.Entry(window, width=45, textvariable=var_dir_path) entry_dir_path.place(x=100, y=20) entry_search_word = ttk.Entry(window, width=45, textvariable=var_search_word) entry_search_word.place(x=100, y=50) # 按钮 btn_search = ttk.Button(window, text='搜索', width=10, command=search_func) btn_search.place(x=480, y=20) btn_select_dir = ttk.Button(window, text='选择路径', width=10, command=lambda:var_dir_path.set(filedialog.askdirectory())) btn_select_dir.place(x=480, y=50) # 文本框 result_text = tk.Text(window, width=60, height=20) result_text.place(x=20, y=80) # 主循环 window.mainloop()



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有