python 实现提取PPT中所有的文字

您所在的位置:网站首页 如何提取ppt中的文字内容 python 实现提取PPT中所有的文字

python 实现提取PPT中所有的文字

#python 实现提取PPT中所有的文字| 来源: 网络整理| 查看: 265

我就废话不多说了,大家还是直接看代码吧~

# 导入pptx包 from pptx import Presentation prs = Presentation(path_to_presentation) text_runs = [] for slide in prs.slides: for shape in slide.shapes: if not shape.has_text_frame: continue for paragraph in shape.text_frame.paragraphs: for run in paragraph.runs: text_runs.append(run.text)

补充:使用 python-pptx-interface 将PPT转换成图片

▌00 简单方法

最简单的方法就是使用PPTX的File中的SaveAs命令,将PPTX文件另存为JPEG格式。

▲ 使用PPT的SaveAs将PPTX存储为JPEG

注意,在最后一步的时候需要选择“所有幻灯片(A)”。

▲ 选择所有幻灯片

最后,PPTX的每张幻灯片都以独立文件方式保存到文件中。X

这部分的内容可以参照: How to Export PowerPoint Slides as JPG or Other Image Formats 中的介绍。

▌01 使用Python-PPTX 1.简介

python-pptx是用于创建和更新PointPoint(PPTX)文件的Python库。

一种常用的场合就是从数据库内容生成一个客户定制的PointPoint文件,这个过程通过点击WEB应用上的连接完成。许多开发之 通过他们日常管理系统生成工程状态汇报PPT。它也可以用于批量生成PPT或者产品特性说明PPT。

python-ppt License:

The MIT License (MIT) Copyright © 2013 Steve Canny, https://github.com/scanny

Python-PPTX对应的官方网络网址: Python-PPTX https://python-pptx.readthedocs.io/en/latest/user/intro.html#

2.安装

使用pip进行安装:

pip install python-pptx

对于python要求: Python2.7,3.3,3.4,3.6

依赖库:

Python 2.6, 2.7, 3.3, 3.4, or 3.6 lxml Pillow XlsxWriter (to use charting features) ▌02 测试

下面的例子来自于: Get Start 。

1. Hello Word from pptx import Presentation prs = Presentation() title_slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(title_slide_layout) title = slide.shapes.title subtitle = slide.placeholders[1] title.text = 'Hello world!' subtitle.text = 'python-pptx was here.' prs.save(r'd:\temp\test.pptx') printf("\a")

2.Add_TextBox from pptx import Presentation from pptx.util import Inches, Pt prs = Presentation() blank_slide_layout = prs.slide_layouts[6] slide = prs.slides.add_slide(blank_slide_layout) left = top = width = height = Inches(1) txBox = slide.shapes.add_textbox(left, top, width, height) tf = txBox.text_frame tf.text = "This is text inside a textbox" p = tf.add_paragraph() p.text = "This is a second paragraph that's bold" p.font.bold = True p = tf.add_paragraph() p.text = "This is a third paragraph that's big" p.font.size = Pt(40) prs.save(r'd:\temp\test1.pptx')

▌03 输出JPEG 1.安装 python-pptx-interface pip install python-pptx-interface 2.转换PPTX

注意:转换生成的目录必须使用新的目录。否则就会出现:

Folder d:\temp\pptimage already exists. Set overwrite_folder=True, if you want to overwrite folder content.

from pptx_tools import utils pptfile = r'D:\Temp\如何搭建自己的电子实验室_20210102R10.pptx' png_folder = r'd:\temp\pptimage' utils.save_pptx_as_png(png_folder, pptfile, overwrite_folder=True)

生成后的PPT对应的PNGImage。

▲ 生成后的PPTX对应的PNG图片

※ 结论

将PPTX转换成图片,可以便于后期将文件上载到CSDN,或者用于DOP文件的制作。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

您可能感兴趣的文章:Python办公自动化PPT批量转换操作利用Python制作PPT的完整步骤python 批量将PPT导出成图片集的案例python自动化办公操作PPT的实现分步骤教你用python一步步提取PPT中的图片


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3