打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python查找文件夹下含有特定关键字的文档word 查找word中的关键词等

应用场景:我们学校搞综测会发放一个压缩包,里面含有各种活动的加分证明,想要快速且不遗漏的找到自己的名字的话,就可以来跑一下这个py啦!

环境:jupyter

要求:事先安装 pip install python-docx

代码修改处:文件存放根目录和要搜索的关键词

  1. #coding=utf-8
  2. from docx import Document
  3. import os,sys
  4. def search_word(filename,word):
  5. #打开文档
  6. document = Document(filename)
  7. #读取每段资料
  8. pa=document.paragraphs
  9. #拼接每一段的内容
  10. para_list = []
  11. for para in pa:
  12. para_list.append(para.text)
  13. #合并字符串
  14. file_text = ''.join(para_list)
  15. if word in file_text:
  16. print("find file for {}:".format(word))
  17. print(filename)
  18. process_list=[]
  19. def get_process_files(root_dir):
  20. """process all files in directory"""
  21. cur_dir=os.path.abspath(root_dir)
  22. file_list=os.listdir(cur_dir)
  23. for file in file_list:
  24. fullfile=cur_dir+"\\"+file
  25. if os.path.isfile(fullfile):
  26. process_list.append(fullfile)
  27. elif os.path.isdir(fullfile):
  28. dir_extra_list=get_process_files(fullfile)
  29. return process_list
  30. def find_files(root_dir,word):
  31. get_process_files(root_dir)
  32. print("总计文件数:{}".format(len(process_list)))
  33. for files in process_list:
  34. search_word(files, word)
  35. return
  36. if __name__=='__main__':
  37. #此处修改自己存放文件位置和名字
  38. #文件根目录
  39. root_dir=r'D:\beizhai\study\大四\综测\【2021】XX学院综测公示文件'
  40. #要搜索的关键字
  41. word="beizhai"
  42. count=0
  43. try:
  44. find_files(root_dir,word)
  45. except:
  46. pass
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
一台电脑上同时安装两个版本的UG
cmd下查看进程路径
Joey's Notes: The Red Hat Linux Boot Process LG #156
FreeBSD常用命令详解与使用手册
python获取文件及文件夹大小
Python合并同一个文件夹下所有PDF文件的方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服