您的位置:首页技术文章
文章详情页

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

【字号: 日期:2022-06-26 08:07:31浏览:5作者:猪猪

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

# 导入pptx包from pptx import Presentationprs = 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格式。

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

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

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

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

▲ 选择所有幻灯片

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

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

?01 使用Python-PPTX1.简介

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.6lxmlPillowXlsxWriter (to use charting features)?02 测试

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

1. Hello Word

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

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

2.Add_TextBox

from pptx import Presentationfrom pptx.util import Inches, Ptprs = 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_frametf.text = 'This is text inside a textbox'p = tf.add_paragraph()p.text = 'This is a second paragraph that’s bold'p.font.bold = Truep = tf.add_paragraph()p.text = 'This is a third paragraph that’s big'p.font.size = Pt(40)prs.save(r’d:temptest1.pptx’)

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

?03 输出JPEG1.安装 python-pptx-interface

pip install python-pptx-interface2.转换PPTX

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

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

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

生成后的PPT对应的PNGImage。

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

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

※ 结论

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

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

标签: python
相关文章: