python 實(shí)現(xiàn)提取PPT中所有的文字
我就廢話不多說了,大家還是直接看代碼吧~
# 導(dǎo)入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)
補(bǔ)充:使用 python-pptx-interface 將PPT轉(zhuǎn)換成圖片
?00 簡單方法最簡單的方法就是使用PPTX的File中的SaveAs命令,將PPTX文件另存為JPEG格式。
▲ 使用PPT的SaveAs將PPTX存儲為JPEG
注意,在最后一步的時(shí)候需要選擇“所有幻燈片(A)”。
▲ 選擇所有幻燈片
最后,PPTX的每張幻燈片都以獨(dú)立文件方式保存到文件中。X
這部分的內(nèi)容可以參照: How to Export PowerPoint Slides as JPG or Other Image Formats 中的介紹。
?01 使用Python-PPTX1.簡介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對應(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
對于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 Wordfrom 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')
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’)
pip install python-pptx-interface2.轉(zhuǎn)換PPTX
注意:轉(zhuǎn)換生成的目錄必須使用新的目錄。否則就會出現(xiàn):
Folder d:temppptimage already exists. Set overwrite_folder=True, if you want to overwrite folder content.
from pptx_tools import utilspptfile = r’D:Temp如何搭建自己的電子實(shí)驗(yàn)室_20210102R10.pptx’png_folder = r’d:temppptimage’utils.save_pptx_as_png(png_folder, pptfile, overwrite_folder=True)
生成后的PPT對應(yīng)的PNGImage。
▲ 生成后的PPTX對應(yīng)的PNG圖片
※ 結(jié)論將PPTX轉(zhuǎn)換成圖片,可以便于后期將文件上載到CSDN,或者用于DOP文件的制作。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐2. Django程序的優(yōu)化技巧3. XML入門的常見問題(一)4. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)5. jsp EL表達(dá)式詳解6. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子7. chat.asp聊天程序的編寫方法8. Python多線程操作之互斥鎖、遞歸鎖、信號量、事件實(shí)例詳解9. idea設(shè)置自動導(dǎo)入依賴的方法步驟10. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
