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

前言

本文对使用python读取pdf、word、excel、ppt、csv、txt等常用文件,并提取所有文本的方法进行分享和使用总结。

可以读取不同文件的库和方法当然不止下面分享的这些,本文的代码主要目标都是:方便提取文件中所有文本的实现方式。

这些库的更多使用方法,请到官方文档中查阅。

读取PDF文本:PyPDF2

python 复制代码
import PyPDF2

def 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

python 复制代码
import docx2txt

def read_docx_to_text(file_path):
    text = docx2txt.process(file_path)
    return text

read_docx_to_text('xxx.docx')

读取excel文本:pandas

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

python 复制代码
import pandas as pd

def read_excel_to_text(file_path):
    excel_file = pd.ExcelFile(file_path)
    sheet_names = excel_file.sheet_names

    text_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

python 复制代码
from pptx import Presentation

def 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_frame
                text = text_frame.text
                if text:
                    text_list.append(text)
    return '\n'.join(text_list)

read_pptx_to_text('xxx.pptx')

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

python 复制代码
def read_txt_to_text(file_path):
    with open(file_path, 'r') as f:
        text = f.read()
    return text

read_txt_to_text('xxx.csv')
read_txt_to_text('xxx.txt')

读取任何文件格式

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

python 复制代码
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 text

read_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')

结语

以上就是全部常见的文件格式的读取和提取所有文本的全部内容了。

更多其他的使用方法请查阅官方文档。

相关推荐
老胖闲聊4 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1184 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之5 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
lyaihao6 小时前
使用python实现奔跑的线条效果
python·绘图
ai大师6 小时前
(附代码及图示)Multi-Query 多查询策略详解
python·langchain·中转api·apikey·中转apikey·免费apikey·claude4
小小爬虾7 小时前
关于datetime获取时间的问题
python
空中湖8 小时前
文档极速转换器 - 免费批量Word转PDF工具
pdf·word
蓝婷儿8 小时前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
沉到海底去吧Go8 小时前
【工具教程】PDF电子发票提取明细导出Excel表格,OFD电子发票行程单提取保存表格,具体操作流程
pdf·excel
chao_7898 小时前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表