python对不同类型文件的字符查找

TXT文件:

    def txt_handler(self, f_name, find_str):
"""
处理txt文件
:param file_name:
:return:
"""
line_count = 1;
file_str_dict = {}
if os.path.exists(f_name):
f = open(f_name, 'r', encoding='utf-8')
for line in f :
if find_str in line:
file_str_dict['file_name'] = f_name
file_str_dict['line_count'] = line_count
break
else:
line_count += 1
return file_str_dict

docx文件

需要用到docx包

pip install python-docx
参考https://python-docx.readthedocs.io/en/latest/
from docx import Document

def docx_handler(self, f_name, find_str):
"""
处理word docx文件
:param file_name:
:return:
"""
# line_count = 1;
file_str_dict = {}
if os.path.exists(f_name):
document = Document(f_name) # 打开文件x.docx
for paragraph in document.paragraphs: # 每个获取段落
# print(paragraph.text)
if find_str in paragraph.text:
file_str_dict['file_name'] = f_name
# file_str_dict['line_count'] = line_count
break return file_str_dict

doc文件:

python没有专门处理doc文件的包,需要把doc转换成docx,再用docx文件类型方式进行处理

from win32com import client as wc

def doc_to_docx(self, fileName):
# 将doc转换成docx
word = wc.Dispatch("Word.Application")
doc = word.Documents.Open(fileName)
# 使用参数16表示将doc转换成docx,保存成docx后才能 读文件
FileNameDocx = fileName[:-4] + '.docx'
doc.SaveAs(FileNameDocx, 16)
doc.Close()
word.Quit()
return FileNameDocx

pdf文件:

这里使用PDFMiner包

python3安装

python -m pip install pdfminer.six 

参考文章

https://dzone.com/articles/exporting-data-from-pdfs-with-python

import io
from pdfminer.converter import TextConverter
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfpage import PDFPage def pdf_handler(self, f_name, find_str):
"""
处理pdf文件
:param file_name:
:return:
"""
# line_count = 1;
file_str_dict = {}
if os.path.exists(f_name):
# pdf = pdfplumber.open(f_name) # 打开文件x.pdf
for page in self.extract_text_by_page(f_name):
# 获取当前页面的全部文本信息,包括表格中的文字
if find_str in page:
file_str_dict['file_name'] = f_name
# file_str_dict['line_count'] = line_count
break
return file_str_dict @staticmethod
def extract_text_by_page(pdf_path):
"""
按页读取PDF
生成器函数按页生成(yield)了文本
:param pdf_path:
:return:
"""
with open(pdf_path, 'rb') as fh:
for page in PDFPage.get_pages(fh,
caching=True,
check_extractable=True):
resource_manager = PDFResourceManager()
fake_file_handle = io.StringIO()
converter = TextConverter(resource_manager, fake_file_handle)
page_interpreter = PDFPageInterpreter(resource_manager, converter)
page_interpreter.process_page(page)
text = fake_file_handle.getvalue()
yield text # 使用生成器
# close open handles
converter.close()
fake_file_handle.close()

python对不同类型文件(doc,txt,pdf)的字符查找的更多相关文章

  1. doc或docx(word)或image类型文件批量转PDF脚本

    doc或docx(word)或image类型文件批量转PDF脚本 1.实际生产环境中遇到文件展示只能适配PDF版本的文件,奈何一万个文件有七千个都是word或者image类型的,由此搞个脚本批量转换下 ...

  2. python反编译chm文件并生成pdf文件

    # -*- coding: utf-8 -*- import os import os.path import logging import pdfkit original_chm = r'C:\Us ...

  3. python基础——python解析yaml类型文件

    一.yaml介绍 yaml全称Yet Another Markup Language(另一种标记语言).采用yaml作为配置文件,文件看起来直观.简洁.方便理解.yaml文件可以解析字典.列表和一些基 ...

  4. 【python】实例-创建文件并通过键盘输入字符

    import os lnend=os.linesep ##windows行结束符号是“\r\n” FileName=raw_input("please input filename:&quo ...

  5. python数据处理(三)之处理pdf文件

    代码以及资料 https://github.com/jackiekazil/data-wrangling 1.前言 尽可能地寻找可以替代pdf格式的数据 2.解析pdf的编程方法 安装slate pi ...

  6. python基础——元组、文件及其它

    Python核心数据类型--元组 元组对象(tuple)是序列,它具有不可改变性,和字符串类似.从语法上讲,它们便在圆括号中,它们支持任意类型.任意嵌套及常见的序列操作. 任意对象的有序集合:与字符串 ...

  7. solr6.6 导入 pdf/doc/txt/json/csv/xml文件

    文本主要介绍通过solr界面dataimport工具导入文件,包括pdf.doc.txt .json.csv.xml等文件,看索引结果有什么不同.其实关键是managed-schema.solrcon ...

  8. python第六篇文件处理类型

    阅读目录 一 文件操作 二 打开文件的模式 三 操作文件的方法 四 文件内光标移动 五 文件的修改   文件处理                                             ...

  9. [大数据]-Fscrawler导入文件(txt,html,pdf,worf...)到Elasticsearch5.3.1并配置同义词过滤

    fscrawler是ES的一个文件导入插件,只需要简单的配置就可以实现将本地文件系统的文件导入到ES中进行检索,同时支持丰富的文件格式(txt.pdf,html,word...)等等.下面详细介绍下f ...

随机推荐

  1. win10如何找回自带的照片查看器

    1.接着我们在打开的记事本中一字不差的输入以下代码 Windows Registry Editor Version 5.00 ; Change Extension's File Type [HKEY_ ...

  2. P5021 赛道修建[贪心+二分]

    题目描述 C 城将要举办一系列的赛车比赛.在比赛前,需要在城内修建 mm 条赛道. C 城一共有 nn 个路口,这些路口编号为 1,2,-,n1,2,-,n,有 n-1n−1 条适合于修建赛道的双向通 ...

  3. 用Python打造了一个渗透测试暴力探测器

    资源探测的作用 通过资源探测,我们可以在目标系统中发现文件.目录.活动.服务还有相关的参数,为下一步的行动提供信息参考. 一个开源的模糊测试数据库 https://github.com/fuzzdb- ...

  4. JUnit 4.x 知识点

    注解 @Test: 测试方法,在这里还可以测试期望异常和超时时间. @Before: 每个测试方法执行之前执行的方法. @BeforeClass: 一个测试类中所有测试方法执行之前执行的方法,只执行一 ...

  5. P5325 【模板】Min_25筛

    题意:定义积性函数f(x)f(x)f(x),且f(p^k)=p^k*(p^k−1)   (p是一个质数),求f(1)+f(2)+...f(n); 思路:板子题.重新打了一份装起来. /* 定义积性函数 ...

  6. oracle数据库(五)

    PL/SQL基础 PL/SQL语言是对oracle数据库语言的扩展,是一种高性能的基于处理事务的语言,能运行在任何oracle环境中,支持所有数据处理命令,支持所有SQL数据类型和函数,支持所有ora ...

  7. 2017EC Final L SOS——找规律&&博弈

    题意 有n个格子排成一行,两人轮流填,可填入"S"或"0",先得到"SOS"的人胜:如果全部填完也没有出现"SOS",则 ...

  8. Nuxt项目支持import写法的最新解决方案

    最近在看Nuxt开发vue项目的视频,视频中讲到Nuxt项目不支持es6的import写法.并提供了解决方案: 1.在package.json中添加我标红的部分: "scripts" ...

  9. Codeforces Round #603 (Div. 2) B. PIN Codes

    链接: https://codeforces.com/contest/1263/problem/B 题意: A PIN code is a string that consists of exactl ...

  10. pg_flame postgresql EXPLAIN ANALYZE 火焰图工具

    pg_flame 是golang 编写的一个将pg的EXPLAIN ANALYZE 转换为火焰图,使用简单 以下是一个简单的demo 环境准备 docker-compose 文件   version: ...