最近被新闻专业同学问起一个很实用的需求,大致是这样的:一个文件夹内有很多文件,怎么通过搜索关键字就可以找到内容包含关键字的word文件,同时获得关键字所在段落的内容。由于最近在学习Python,所以对这个需求很感兴趣,于是当时很快就把这个功能做出来了。

首先想到的自然是用脚本实现,新建一个kword.py文件,差不读四十几行代码将可以实现,步骤是这样的:

1.先获取文件夹里面的所有文件路径、其中用递归算法获取子文件夹内的文件路径:

def get_process_files(root_dir):

"""process all files in directory"""

cur_dir=os.path.abspath(root_dir)

file_list=os.listdir(cur_dir)

process_list=[]

for file in file_list:

fullfile=cur_dir+"\\"+file

if os.path.isfile(fullfile):

process_list.append(fullfile)

elif os.path.isdir(fullfile):

dir_extra_list=get_process_files(fullfile)

if len(dir_extra_list)!=0:

for x in dir_extra_list:

process_list.append(x)

return process_list

2.利用python-docx模块中的Document方法对word文件中每一段进行关键字匹配:

def search_word(filename,word):

#打开文档

document = Document(filename)

# document = Document(r'C:\Users\Cheng\Desktop\kword\words\wind.docx')

print filename

#读取每段资料

l = [ paragraph.text.encode('gb2312') for paragraph in document.paragraphs];

#输出并观察结果,也可以通过其他手段处理文本即可

for i in l:

i=i.strip()

# print i

if i.find(word)!=-1:

print filename, i

3.遍历每一个文件并进行查找:

def find_files(root_dir,word):

process_list=get_process_files(root_dir)

for files in process_list:

search_word(files, word)

这里有两个参数需要我们自己输入,分别是文件目录和关键字。

#文件根目录

root_dir=sys.argv[1]

#要搜索的关键字

word=sys.argv[2]

至此,在脚本目录下运行“python kword.py 目录 关键字”命令就可以看到搜索结果,目前搜索功能比较简单,没有做大量文件测试和算法优化。

献上脚本源码:

#coding=utf-8

from docx import Document

import os,sys

def search_word(filename,word):

#打开文档

document = Document(filename)

# document = Document(r'C:\Users\Cheng\Desktop\kword\words\wind.docx')

print filename

#读取每段资料

l = [ paragraph.text.encode('gb2312') for paragraph in document.paragraphs];

#输出并观察结果,也可以通过其他手段处理文本即可

for i in l:

i=i.strip()

# print i

if i.find(word)!=-1:

print filename, i

def get_process_files(root_dir):

"""process all files in directory"""

cur_dir=os.path.abspath(root_dir)

file_list=os.listdir(cur_dir)

process_list=[]

for file in file_list:

fullfile=cur_dir+"\\"+file

if os.path.isfile(fullfile):

process_list.append(fullfile)

elif os.path.isdir(fullfile):

dir_extra_list=get_process_files(fullfile)

if len(dir_extra_list)!=0:

for x in dir_extra_list:

process_list.append(x)

return process_list

def find_files(root_dir,word):

process_list=get_process_files(root_dir)

for files in process_list:

search_word(files, word)

if __name__=='__main__':

#文件根目录

root_dir=sys.argv[1]

#要搜索的关键字

word=sys.argv[2]

try:

find_files(root_dir,word)

except:

pass

如果你喜欢本文章,还请点个关注和喜欢,我会为大家不断地带来Python学习笔记。

python操作word 查找_Python实现搜索关键字定位文件01相关推荐

  1. python 操作word教程_Python 使用 win32com 模块对word文件进行操作

    what's the win32com 模块 win32com 模块主要为 Python 提供调用 windows 底层组件对 word .Excel.PPT 等进行操作的功能,只能在 Windows ...

  2. python操作word填表_Python 自动化办公—Word 文本操作命令

    点击上方 小张Python,加为星标 第一时间收到 Python 技术干货! 之前介绍了一个Python包 openpyxl ,用于处理 Excel :而对于 Word 文本时同样也有对应的 Pyth ...

  3. python操作word文档中的图片_Python操作word文档插入图片和表格的实例演示

    前言P6Q免费资源网 图片是Word的一种特殊内容,这篇文章主要介绍了关于Python操作word文档,向里面插入图片和表格的相关内容,下面话不多说了,来一起看看详细的代码P6Q免费资源网 实例代码: ...

  4. python操作word详细操作_Python操作Word的入门教程

    Python操作Word的入门教程 前言 今天来介绍下,如何用 Python 来操作 Word. 再来介绍操作 Word 之前,先来说一个最近看书学到的法则,即 3W 法则. 3W:3W分别指 Wha ...

  5. Python操作word手册,与打包生成word文件

    title: Python操作word手册 copyright: true top: 0 date: 2018-11-23 14:59:03 tags: python-word categories: ...

  6. 【python操作word和pdf】

    python操作word和pdf 一.python操作word 1.python新建word文档及常规操作 2.python对word的字体设置 3.python向word插入图片和表格 4.pyth ...

  7. php怎么在表格里插图片,Python操作word文档插入图片和表格的实例演示

    今天带来Python操作word文档插入图片和表格的实例演示教程详解 前言 图片是Word的一种特殊内容,这篇文章主要介绍了关于Python操作word文档,向里面插入图片和表格的相关内容,下面话不多 ...

  8. python操作word文档(python-docx)

    python操作word文档(python-docx) 1. 效果图 1.1 python-docx文档标题段落(等级.加粗.斜体.居中)效果图 1.2 python-docx字体(加粗.斜体.居中. ...

  9. python入门教程2word-使用python操作word

    前言 最近工作中,需要将查询的一段时间的数据可视化,并导出为word格式.由于对word操作不熟悉,查阅了一下相关文档,这里简要记录一下如何使用python操作word. 说明 该代码记录了对word ...

  10. python入门教程2word-入门干货:Python操作Word文件经验分享

    原标题:入门干货:Python操作Word文件经验分享 导读:Microsoft Word在当前使用中是占有巨大优势的文字处理器,这使得Word专用的档案格式Word 文件(.docx)成为事实上最通 ...

最新文章

  1. 运动目标检测、阴影检测及目标跟踪中用得到的标准测试视频下载
  2. python爬虫教程网-Python爬虫全集
  3. 计算机虚拟化技术论文,【计算机网络论文】虚拟技术计算机网络论文(共1775字)...
  4. 文件断点续传原理与实现
  5. php循环读取mysql_PHP无限循环获取MySQL数据
  6. 动态规划 NOIP经典问题 开心的金明(洛谷P1060题题解,Java语言描述)
  7. 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 4:Debugging: Gradient Checking
  8. 学习Altas 笔记[js调用重载的方法出错,如何处理]
  9. 1.Zabbix企业级分布式监控系统 --- 监控系统简介
  10. 前端jquery学习
  11. 204.计数质数 (力扣leetcode) 博主可答疑该问题
  12. 微信小程序如何使用阿里矢量库
  13. Python编程 | 统计新浪微博热门话题
  14. 固态硬盘SSD的接口如何选择
  15. 活动倒计时的一些想法
  16. iOS 获取手机IP 地址
  17. 如何搭建属于自己的腾讯云服务器
  18. 数据库——操作(mysql)
  19. 如何在word 插入矢量图
  20. Android的MotionEvent和事件处理

热门文章

  1. 华为端口聚合命令_华为交换机两种端口聚合模式使用实例
  2. java udp判断端口是否打开,java udp 端口
  3. 【如何注销CSDN账号】说容易也容易,说难也难
  4. Spring Boot 1.5.2.RELEASE中文版
  5. 计算机组成原理学习笔记————存储器(一) 存储器分类
  6. Skype 去广告安装方法
  7. 驱动开发入门 - 之二:Win7-x64 + VMWare (Win7-x64) + WinDbg 双机调试环境搭建
  8. listView的最简单的使用方法
  9. html-mp3格式转换器,mp3格式转换器
  10. miui8 android版本,miui8官方正式版下载_小米8系统安卓下载|好特下载