python 實(shí)現(xiàn)提取PPT中所有的文字
我就廢話不多說了,大家還是直接看代碼吧~
# 導(dǎo)入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)
補(bǔ)充:使用 python-pptx-interface 將PPT轉(zhuǎn)換成圖片
▌00 簡單方法
最簡單的方法就是使用PPTX的File中的SaveAs命令,將PPTX文件另存為JPEG格式。
▲ 使用PPT的SaveAs將PPTX存儲(chǔ)為JPEG
注意,在最后一步的時(shí)候需要選擇“所有幻燈片(A)”。
▲ 選擇所有幻燈片
最后,PPTX的每張幻燈片都以獨(dú)立文件方式保存到文件中。X
這部分的內(nèi)容可以參照: How to Export PowerPoint Slides as JPG or Other Image Formats 中的介紹。
▌01 使用Python-PPTX
1.簡介
python-pptx是用于創(chuàng)建和更新PointPoint(PPTX)文件的Python庫。
一種常用的場合就是從數(shù)據(jù)庫內(nèi)容生成一個(gè)客戶定制的PointPoint文件,這個(gè)過程通過點(diǎn)擊WEB應(yīng)用上的連接完成。許多開發(fā)之 通過他們?nèi)粘9芾硐到y(tǒng)生成工程狀態(tài)匯報(bào)PPT。它也可以用于批量生成PPT或者產(chǎn)品特性說明PPT。
python-ppt License:
The MIT License (MIT) Copyright © 2013 Steve Canny, https://github.com/scanny
Python-PPTX對(duì)應(yīng)的官方網(wǎng)絡(luò)網(wǎng)址: Python-PPTX https://python-pptx.readthedocs.io/en/latest/user/intro.html#
2.安裝
使用pip進(jìn)行安裝:
pip install python-pptx
對(duì)于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.轉(zhuǎn)換PPTX
注意:轉(zhuǎn)換生成的目錄必須使用新的目錄。否則就會(huì)出現(xiàn):
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\如何搭建自己的電子實(shí)驗(yàn)室_20210102R10.pptx' png_folder = r'd:\temp\pptimage' utils.save_pptx_as_png(png_folder, pptfile, overwrite_folder=True)
生成后的PPT對(duì)應(yīng)的PNGImage。
▲ 生成后的PPTX對(duì)應(yīng)的PNG圖片
※ 結(jié)論
將PPTX轉(zhuǎn)換成圖片,可以便于后期將文件上載到CSDN,或者用于DOP文件的制作。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
python GUI庫圖形界面開發(fā)之PyQt5復(fù)選框控件QCheckBox詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5復(fù)選框控件QCheckBox詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實(shí)現(xiàn)線性表的順序
這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實(shí)現(xiàn)線性表的順序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09Python標(biāo)準(zhǔn)庫pickle的簡單使用
本文主要介紹了Python標(biāo)準(zhǔn)庫pickle的簡單使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Python 實(shí)現(xiàn)還原已撤回的微信消息
這篇文章主要介紹了Python 神操作,還原已撤回的微信消息功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06