python对不同类型文件(doc,txt,pdf)的字符查找
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)的字符查找的更多相关文章
- doc或docx(word)或image类型文件批量转PDF脚本
doc或docx(word)或image类型文件批量转PDF脚本 1.实际生产环境中遇到文件展示只能适配PDF版本的文件,奈何一万个文件有七千个都是word或者image类型的,由此搞个脚本批量转换下 ...
- python反编译chm文件并生成pdf文件
# -*- coding: utf-8 -*- import os import os.path import logging import pdfkit original_chm = r'C:\Us ...
- python基础——python解析yaml类型文件
一.yaml介绍 yaml全称Yet Another Markup Language(另一种标记语言).采用yaml作为配置文件,文件看起来直观.简洁.方便理解.yaml文件可以解析字典.列表和一些基 ...
- 【python】实例-创建文件并通过键盘输入字符
import os lnend=os.linesep ##windows行结束符号是“\r\n” FileName=raw_input("please input filename:&quo ...
- python数据处理(三)之处理pdf文件
代码以及资料 https://github.com/jackiekazil/data-wrangling 1.前言 尽可能地寻找可以替代pdf格式的数据 2.解析pdf的编程方法 安装slate pi ...
- python基础——元组、文件及其它
Python核心数据类型--元组 元组对象(tuple)是序列,它具有不可改变性,和字符串类似.从语法上讲,它们便在圆括号中,它们支持任意类型.任意嵌套及常见的序列操作. 任意对象的有序集合:与字符串 ...
- solr6.6 导入 pdf/doc/txt/json/csv/xml文件
文本主要介绍通过solr界面dataimport工具导入文件,包括pdf.doc.txt .json.csv.xml等文件,看索引结果有什么不同.其实关键是managed-schema.solrcon ...
- python第六篇文件处理类型
阅读目录 一 文件操作 二 打开文件的模式 三 操作文件的方法 四 文件内光标移动 五 文件的修改 文件处理 ...
- [大数据]-Fscrawler导入文件(txt,html,pdf,worf...)到Elasticsearch5.3.1并配置同义词过滤
fscrawler是ES的一个文件导入插件,只需要简单的配置就可以实现将本地文件系统的文件导入到ES中进行检索,同时支持丰富的文件格式(txt.pdf,html,word...)等等.下面详细介绍下f ...
随机推荐
- PTA 树的遍历(根据后序中序遍历输出层序遍历)
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式:输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行给出其后序遍历序列.第 ...
- 关于PID控制的一点资料搜集
CMU做的控制教程 <动态系统的反馈控制> MATLAB&Simulink的PID控制(官方)
- 第三方百度网盘客户端 PanDownload、速盘、panlight
PanDownload PanDownload是一款能够快速下载百度网盘内资源的强大工具.PanDownload能够无限速高速下载,满速下载百度云盘里的各种资源.而且PanDownload完全免费,免 ...
- woocommerce模板制作简易教程
woocommerce是wordpress里比较好用的电商解决方案,但是制作woocommerce模板相对比较复杂,如果想用woocommerce来建一个展示型的网站,不带下单功能,我们可以很快就能把 ...
- spark运行时加载配置文件(hive,hdfs)
文章为转载,如有版权问题,请联系,谢谢! 转自:https://blog.csdn.net/piduzi/article/details/81636253 适合场景:在运行时才确定用哪个数据源 imp ...
- 测试mybatis延迟加载错误与解决方法
什么是延迟加载? 延迟加载又叫懒加载,也叫按需加载,也就是说先加载主信息,需要的时候,再去加载从信息. 需求: 查询订单信息,需要时再去查询用户信息 实现方式: 编写两个statement,其中一个s ...
- modbus-poll和modbus-slave工具的学习使用——modbus协议功能码3的解析(及欧姆龙温控器调试笔记)
最近的项目中使用到了欧姆龙的温控器,里面有很多的通信方式,我们使用的常见的modbus——RTU方式,其他方式我们不使用,其中通信手册上面有很多通信的实例,欧姆龙modbus还区分4字节模式和2字节模 ...
- iOS App开发那些事:如何选择合适的人、规范和框架?
http://www.cocoachina.com/ios/20141202/10386.html 自从做Team Leader之后,身上权责发生了变化,于是让我烦恼的不再是具体某个功能,某个界面的实 ...
- 牛客挑战赛32 E. 树上逆序对
对于一对 $(x, y)$,能成为逆序对的取决于绝对值大的那个数的符号.假如 $a[x] > a[y]$,当 $a[x]$ 为正时,不管 $a[y]$ 取不取负号都比 $a[x]$ 小.当 $a ...
- rune 数据类型
// rune is an alias for int32 and is equivalent to int32 in all ways. It is // used, by convention, ...