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

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

使用python读取文件,其中pdf、docx、pptx可以直接读,.ppt和.doc文件不能直接读,需要转换成.pptx和.docx文件,并且需要区分系统
如果是linux系统,请先安装组件

#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代码如下:

import osdef read_pptx(fp):import pptxprs = 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_frameprint(str(i) + '页:' + text_frame.text)def read_ppt(fp):import platformos_type = platform.system()if os_type == "Windows":import win32com.client as wcpowerpoint = wc.Dispatch("PowerPoint.Application")wc.gencache.EnsureDispatch("PowerPoint.Application")powerpoint.Visible = 1ppt = powerpoint.Presentations.Open(fp)ppt.SaveAs(fp+"x")powerpoint.Quit()elif os_type == "Linux":import aspose.slides as slideswith slides.Presentation(fp) as presentation:presentation.save(fp+"x", slides.export.SaveFormat.PPTX)read_pptx(fp+"x")def read_docx(fp):import docxfile = docx.Document(fp)print("段落数:" + str(len(file.paragraphs)))  # 段落数为13,每个回车隔离一段# 输出每一段的内容for para in file.paragraphs:print(para.text)def read_doc(fp):import platformos_type = platform.system()if os_type == "Windows":import doc2docxdoc2docx.convert(fp,fp+"x")elif os_type == "Linux":import subprocesssubprocess.check_output(["soffice", "--headless", "–-invisible", "--convert-to", "docx", fp, "--outdir", fp+"x"])read_docx(fp + "x")def read_pdf(fp):import pdfplumberpdfFile = 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)
http://www.lryc.cn/news/136704.html

相关文章:

  • 鸿鹄工程项目管理系统 Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统 em
  • maven无法从阿里云中央仓库下载jar包的解决方法
  • Nevron Open Vision for .NET Crack
  • 玩转单元测试之cppmockfree
  • Open3D点云数据处理(二十一):最小二乘多项式拟合
  • 智能手表:华米稳、华为猛
  • 【日常积累】Linux中vi/vim的使用
  • 基于React实现日历组件详细教程
  • Web安全测试(二):HTTP状态码、响应和url详解
  • 什么是算法评价指标
  • 什么是软件压力测试?软件压力测试工具和流程有哪些?
  • Wireshark流量分析例题
  • 聚观早报|2023戴尔科技峰会助力创新;小米汽车电池供应商敲定
  • 大学生创业出路【第二弹】科创训练营
  • EG3D: Efficient Geometry-aware 3D Generative Adversarial Networks [2022 CVPR]
  • 进行Stable Diffusion的ai训练怎么选择显卡?
  • 【Linux】【驱动】第一个相对完整的驱动编写
  • PHP 高德地图,获取经纬度
  • Node基础--Node基础使用体验
  • 22 从0到1:API测试怎么做?常用API测试工具简介
  • Skywalking Kafka Tracing实现
  • Perl 解析字符串为日期对象并获取多天前的日期字符串
  • C语言问题 - 关于一维数组和二维数组用*a+i形式表达
  • 验证码识别DLL ,滑块识别SDK,OCR图片转文字,机器视觉找物品
  • 【图论】最小生成树的应用
  • C++类模板的特化(三)
  • 基于YOLOV8模型的课堂场景下人脸目标检测系统(PyTorch+Pyside6+YOLOv8模型)
  • java八股文面试[数据结构]——Map有哪些子类
  • 司徒理财:8.23今日黄金原油走势分析附操作策略
  • 使用动态IP是否会影响网络