python读取pdf、doc、docx、ppt、pptx文件内容

使用python读取文件,其中pdf、docx、pptx可以直接读,.ppt和.doc文件不能直接读,需要转换成.pptx和.docx文件,并且需要区分系统

如果是linux系统,请先安装组件

bash 复制代码
#doc2docx
yum install -y libreoffice-headless
yum install -y libreoffice-writer

# ppt2pptx
yum install epel-release -y
yum install libgdiplus -y
pip3 install aspose.slides

python代码如下:

python 复制代码
import os


def read_pptx(fp):
    import pptx
    prs = pptx.Presentation(fp)
    for i, slide in enumerate(prs.slides):
        # if i == 1:  在这里可以指定提取ppt的具体页数
        for shape in slide.shapes:
            if shape.has_text_frame:
                text_frame = shape.text_frame
                print(str(i) + '页:' + text_frame.text)


def read_ppt(fp):
    import platform
    os_type = platform.system()
    if os_type == "Windows":
        import win32com.client as wc
        powerpoint = wc.Dispatch("PowerPoint.Application")
        wc.gencache.EnsureDispatch("PowerPoint.Application")
        powerpoint.Visible = 1
        ppt = powerpoint.Presentations.Open(fp)
        ppt.SaveAs(fp+"x")
        powerpoint.Quit()
    elif os_type == "Linux":
        import aspose.slides as slides
        with slides.Presentation(fp) as presentation:
            presentation.save(fp+"x", slides.export.SaveFormat.PPTX)
    read_pptx(fp+"x")

def read_docx(fp):
    import docx
    file = docx.Document(fp)
    print("段落数:" + str(len(file.paragraphs)))  # 段落数为13,每个回车隔离一段
    # 输出每一段的内容
    for para in file.paragraphs:
        print(para.text)

def read_doc(fp):
    import platform
    os_type = platform.system()
    if os_type == "Windows":
        import doc2docx
        doc2docx.convert(fp,fp+"x")
    elif os_type == "Linux":
        import subprocess
        subprocess.check_output(["soffice", "--headless", "---invisible", "--convert-to", "docx", fp, "--outdir", fp+"x"])
    read_docx(fp + "x")


def read_pdf(fp):
    import pdfplumber
    pdfFile = open(fp,"rb")
    pdf = pdfplumber.open(pdfFile)
    for page in pdf.pages:
        text = page.extract_text()
        print(text)


# file_path = "*.pptx"
file_path = "*.ppt"
# file_path = "*.docx"
# file_path = "*.doc"
# file_path = "*.pdf"
file_extension = os.path.splitext(file_path)[-1].lower()
print("文件后缀:" + file_extension)
if file_extension == '.pptx':
    print("读取pptx文件")
    read_pptx(file_path)
elif file_extension == '.ppt':
    print("读取ppt文件")
    read_ppt(file_path)
elif file_extension == ".docx":
    print("读取docx文件")
    read_docx(file_path)
elif file_extension == ".doc":
    print("读取doc文件")
    read_doc(file_path)
elif file_extension == ".pdf":
    print("读取pdf文件")
    read_pdf(file_path)
相关推荐
eric-sjq9 小时前
小思框架 · 30M 级长上下文新架构对比技术报告
人工智能·python·深度学习·语言模型
reasonsummer10 小时前
【办公类-04-05】20261030整理手机照片(华为手机助手导出照片到日期文件夹+批量添加四级文件夹+手动转移照片更名+更改三级文件夹合并汉字名称)
python·智能手机
BD_Marathon11 小时前
【Python】初识迭代器
python
gz927cool11 小时前
Python 全栈应用最佳实践完整指南
开发语言·python·ai
智购科技智能售货柜11 小时前
顾客已经关门,视觉售货柜还在处理旧画面:延迟积压怎么查?~YH
python·单片机·嵌入式硬件·物联网·eclipse
梯度下降者12 小时前
Python从0到100(三):Python中的变量介绍
python·作用域·变量·数据类型·计算机常识
AIFQuant12 小时前
Python期货行情API接入:股指、黄金、原油多品种数据管道实战
开发语言·python·金融·区块链·量化开发
威哥爱编程12 小时前
Python 智能体开发实战 07:让它查课表它张口就编?给 Agent 接上真正能动手的工具
python·agent
Wang's Blog12 小时前
Java 项目实战:交友项目 - 即时通信总结:全链路复盘、可复用清单与自检
java·python·交友
2601_9628857212 小时前
按主板/科创/创业/北交分板扫描选股
python