import os, time, fnmatch
from docx import Document class search: def __init__(self, path, search_string, file_filter): self.search_path = path
self.search_string = search_string
self.file_filter = file_filter print ("Search %s in %s..." % (
self.search_string, self.search_path
) ) print ("_" * 80)
time_begin = time.time()
file_count = self.walk() print ("_" * 80)
print ("%s files searched in %0.2fsec." % (
file_count, (time.time() - time_begin)
)) #遍历所有的文件,记录文件数量
def walk(self):
file_count = 0
for root, dirlist, filelist in os.walk(self.search_path, followlinks=True):
for filename in filelist:
for file_filter in self.file_filter:
if fnmatch.fnmatch(filename, file_filter):
self.search_file(os.path.join(root, filename))
file_count += 1
return file_count #遍历文件中的字符串,并且剪切显示出来
def search_file(self, filepath):
d = Document(filepath)
for para in d.paragraphs:
if self.search_string in d.paragraphs:
print(filepath)
self.cutout_content(content) #剪切字符串并且显示
def cutout_content(self, content):
current_pos = 0
search_string_len = len(self.search_string)
for i in xrange(max_cutouts):
try:
#从current_pos位置往后寻找self.search_string个字符串
pos = content.index(self.search_string, current_pos)
except ValueError:
break
#将显示窗口定义为寻找到的关键字向前向后各content_extract个字符
content_window = content[ pos - content_extract : pos + content_extract ]
print (">>>", content_window.encode("String_Escape"))
current_pos += pos + search_string_len
print #主程序入口
if __name__ == "__main__":
search_path = r"c:\Users\Administrator\Desktop"
file_filter = ("*.docx",".doc") # fnmatch-Filter
search_string = "history"
content_extract = 35 #获取摘要35
max_cutouts = 20 #显示窗口20
search(search_path, search_string, file_filter)

  

python读取doc的更多相关文章

  1. Python:读取 .doc、.docx 两种 Word 文件简述及“Word 未能引发事件”错误

    概述 Python 中可以读取 word 文件的库有 python-docx 和 pywin32. 下表比较了各自的优缺点.   优点 缺点 python-docx 跨平台 只能处理 .docx 格式 ...

  2. 【转】Python——读取html的table内容

    Python——python读取html实战,作业7(python programming) 查看源码,观察html结构 # -*- coding: utf-8 -*- from lxml.html ...

  3. 孤荷凌寒自学python第五十二天初次尝试使用python读取Firebase数据库中记录

    孤荷凌寒自学python第五十二天初次尝试使用python读取Firebase数据库中记录 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数 ...

  4. python读取excel一例-------从工资表逐行提取信息

    在工作中经常要用到python操作excel,比如笔者公司中一个人事MM在发工资单的时候,需要从几百行的excel表中逐条的粘出信息,然后逐个的发送到员工的邮箱中.人事MM对此事不胜其烦,终于在某天请 ...

  5. python读取xml文件

    关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...

  6. python读取mnist

    python读取mnist 其实就是python怎么读取binnary file mnist的结构如下,选取train-images TRAINING SET IMAGE FILE (train-im ...

  7. [转] Windows下使用Python读取Excel表格数据

    http://www.python-excel.org/这个网站罗列了很多关于在Python下操作Excel文件的信息,这里选择了其介绍的第一个模块xlrd . xlrd 0.9.2版本跨平台同时支持 ...

  8. Python读取txt文件

    Python读取txt文件,有两种方式: (1)逐行读取 data=open("data.txt") line=data.readline() while line: print ...

  9. Python读取Yaml文件

    近期看到好多使用Yaml文件做为配置文件或者数据文件的工程,随即也研究了下,发现Yaml有几个优点:可读性好.和脚本语言的交互性好(确实非常好).使用实现语言的数据类型.有一个一致的数据模型.易于实现 ...

随机推荐

  1. python中logging的常用方法

    logging常用 # -*- coding:utf-8 -*- __author__ = "lgj" import os import sys import time impor ...

  2. tomcat web.log 系统日志记录文件过大问题修改

    目前各系统都是记录所有的日志,产生日志文件太大,按照如下设置修改log4j.properties文件:其中橙色部分为系统名称,例如water-scada系统,名称可以为scada. #Sun Jun ...

  3. URAL 1936 Roshambo(求期望)

    Description Bootstrap: Wondering how it's played? Will: It's a game of deception. But your bet inclu ...

  4. mysql初始(6)

    随着mysql的运用不断加深,一些更复杂点的用法又需要总结起来. 1.将一个表中的数据插入到另一个表中: a.两张表字段相同,并且数据全部插入,命令如下:  INSERT INTO 目标表 SELEC ...

  5. js中迭代元素特性与DOM中的DocumentFragment类型 笔记

    JS中迭代元素特性 在需要将DOM结构序列化为XML或者HTML字符串时,多数都会涉及遍历元素的特性,这个时候attributes属性就可以派上用场. 以下代码展示了如何迭代元素的每一个特性,然后将他 ...

  6. JQuery JTable根据某行的某个值来设置行的背景颜色

    目录 描述 处理方法 参考 描述 某个表的数据是用JQuery的JTable插件进行展示的.现在需求是:当表中的master字段为true时,就将对应的整行的背景颜色设置为浅蓝色. 处理方法 在fie ...

  7. vue2.0介绍

    1.vue.js 是什么 vue(view)是一套构建用户界面的渐进式框架 Vue (pronounced /vjuː/, like view) is a progressive framework  ...

  8. hdu6121 build a tree(树)

    题解: 可以考虑每一层结点的子树大小 必定满足下面的情况,即 a,a,a,a,a,a,b,c,c,c,c........ 然后每一层依次往上更新,结果是不变的 一共有logn层,所以依次扫上去,统计结 ...

  9. 【题解】NOI2009二叉查找树 + NOIP2003加分二叉树

    自己的思维能力果然还是太不够……想到了这棵树所有的性质即中序遍历不变,却并没有想到怎样利用这一点.在想这道题的过程中走入了诸多的误区,在这里想记录一下 & 从中吸取到的教训(原该可以避免的吧) ...

  10. [Leetcode] text justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...