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. Visual Studio 2012安装包

    点击下载

  2. swarm 服务器安装

    apt install docker.io-----------------------配置加速器.私有仓库地址---------------------mkdir -p /etc/dockertee ...

  3. PAT 甲级 1003 Emergency

    https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376 As an emergency rescue ...

  4. MD5消息摘要算法

    Message Digest 5(消息摘要算法)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护. MD5的作用是让大容量信息在用数字签名软件签署私人密钥前被"压缩" ...

  5. java对象-String的用法

    以某字符串结尾:“”.endwith("") 字符串比较:equals(anotherstr) 命名遵循英文语法

  6. 解决jQuery load()加载GB2312页面时出现乱码

    jquery的字符集是utf-8,load方法加载完GB2312编码静态页面后,出现中文乱码. 这是jQueryAJAX.html <!DOCTYPE html PUBLIC "-// ...

  7. hihocoder 1323 回文字符串(字符串+dp)

    题解: 比较水的题目 dp[i][j]表示[i...j]最少改变几次变成回文字符串 那么有三种转移 dp[i][j] = dp[i+1][j-1] + s[i] != s[j] dp[i][j] = ...

  8. [bzoj5285] [HNOI2018]寻宝游戏

    Description 某大学每年都会有一次Mystery Hunt的活动,玩家需要根据设置的线索解谜,找到宝藏的位置,前一年获胜的队伍可以获得这一年出题的机会. 作为新生的你,对这个活动非常感兴趣. ...

  9. ARC074 E RGB Sequence DP

    ---题面--- 题解: 首先,有一个不太直观的状态,f[i][j][k][l]表示DP到i位,三种颜色最后出现的位置分别是j, k, l的方案数.因为知道了三种颜色最后出现的位置,因此也可以得知以当 ...

  10. Android逆向之旅---爆破一款资讯类应用「最右」防抓包策略原理分析

    一.逆向分析 首先感谢王同学提供的样本,因为王同学那天找到我咨询我说有一个应用Fiddler抓包失败,其实对于这类问题,我一般都会这么回答:第一你是否安装Fiddler证书了,他说他安装了.第二你是否 ...