脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python 实现提取PPT中所有的文字

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

2021-09-16 00:35啊呀啊呀静 Python

这篇文章主要介绍了python 实现提取PPT中所有的文字,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

# 导入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格式。

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

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

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

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

▲ 选择所有幻灯片

最后,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")

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

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

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

▌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。

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

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

※ 结论

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

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

原文链接:https://blog.csdn.net/jeanjing222/article/details/108283323

延伸 · 阅读

精彩推荐