当前位置: 首页 > news >正文

Python - 读取pdf、word、excel、ppt、csv、txt文件提取所有文本

前言

本文对使用python读取pdf、word、excel、ppt、csv、txt等常用文件,并提取所有文本的方法进行分享和使用总结。
可以读取不同文件的库和方法当然不止下面分享的这些,本文的代码主要目标都是:方便提取文件中所有文本的实现方式。
这些库的更多使用方法,请到官方文档中查阅。

读取PDF文本:PyPDF2

import PyPDF2def read_pdf_to_text(file_path):with open(file_path, 'rb') as pdf_file:pdf_reader = PyPDF2.PdfReader(pdf_file)contents_list = []for page in pdf_reader.pages:content = page.extract_text()contents_list.append(content)return '\n'.join(contents_list)read_pdf_to_text('xxx.pdf')

读取Word文本:docx2txt

doc需先手动转换成docx

import docx2txtdef read_docx_to_text(file_path):text = docx2txt.process(file_path)return textread_docx_to_text('xxx.docx')

读取excel文本:pandas

当然,pandas能读取的文件不仅仅是excel,还包括csv、json等。

import pandas as pddef read_excel_to_text(file_path):excel_file = pd.ExcelFile(file_path)sheet_names = excel_file.sheet_namestext_list = []for sheet_name in sheet_names:df = excel_file.parse(sheet_name)text = df.to_string(index=False)text_list.append(text)return '\n'.join(text_list)read_excel_to_text('xxx.xlsx')

读取ppt文本:pptx

from pptx import Presentationdef read_pptx_to_text(file_path):prs = Presentation(file_path)text_list = []for slide in prs.slides:for shape in slide.shapes:if shape.has_text_frame:text_frame = shape.text_frametext = text_frame.textif text:text_list.append(text)return '\n'.join(text_list)read_pptx_to_text('xxx.pptx')

读取csv、txt其他文本:直接open,read()

def read_txt_to_text(file_path):with open(file_path, 'r') as f:text = f.read()return textread_txt_to_text('xxx.csv')
read_txt_to_text('xxx.txt')

读取任何文件格式

有了前面的所有函数,那我们可以写一个支持传任意格式文件的函数。

support = {'pdf': 'read_pdf_to_text','docx': 'read_docx_to_text','xlsx': 'read_excel_to_text','pptx': 'read_pptx_to_text','csv': 'read_txt_to_text','txt': 'read_txt_to_text',
}def read_any_file_to_text(file_path):file_suffix = file_path.split('.')[-1]func = support.get(file_suffix)if func is None:return '暂不支持该文件格式'text = eval(func)(file_path)return textread_any_file_to_text('xxx.pdf')
read_any_file_to_text('xxx.docx')
read_any_file_to_text('xxx.xlsx')
read_any_file_to_text('xxx.pptx')
read_any_file_to_text('xxx.csv')
read_any_file_to_text('xxx.txt')

结语

以上就是全部常见的文件格式的读取和提取所有文本的全部内容了。
更多其他的使用方法请查阅官方文档。

http://www.lryc.cn/news/121219.html

相关文章:

  • Codeforces Round 892 (Div. 2) C. Another Permutation Problem 纯数学方法 思维题
  • 持续输出:自媒体持续输出文字内容、视音频创作(视频课程、书籍章节)
  • 篇十七:备忘录模式:恢复对象状态
  • 初识mysql数据库之图形化界面
  • APP外包开发的H5开发框架
  • 高性能跨平台网络通信框架 HP-Socket v5.9.3
  • Vue3.2+TS在v-for的时候,循环处理时间,将其变成xx-xx-xx xx:xx:xx格式,最后教给大家自己封装一个时间hooks,直接复用
  • 05 mysql innodb page
  • 记录一次electron打包提示文件找不到的解决方法
  • 《大型网站技术架构》第二篇 架构-高可用
  • VS Code 使用cnpm下载包失败
  • 【图像分类】CNN + Transformer 结合系列.4
  • 分享一下利用Vue表单处理实现复杂表单布局
  • SAP Fiori 问题收集
  • econml双机器学习实现连续干预和预测
  • 《甲午》观后感——GPT-3.5所写
  • Java技术整理(6)—— 微服务篇
  • 途乐证券-新股行情持续火爆,哪些因素影响首日表现?
  • 在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——聚合与搜索(三)
  • 基于weka手工实现KNN
  • Lua 闭包
  • Java技术整理(1)—— JVM篇
  • bug解决:AssertionError: No inf checks were recorded for this optimizer.
  • Django笔记之数据库查询优化汇总
  • JVM内存区域
  • 某行业CTF一道流量分析题
  • 【Kafka】1.Kafka简介及安装
  • Kafka API与SpringBoot调用
  • JavaScript构造函数和类的区别
  • Spring与Spring Bean