Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to delete PowerPoint Slides using Python-pptx

I am trying to delete PowerPoint slides containing a specific keywords using Python-pptx. If the keyword is present anywhere in the slide then that slide will be deleted. My code is given below:

from pptx import Presentation

String = 'Macro'

ppt = Presentation('D:\\Shaon\\pptss\\Regional.pptx')

for slide in ppt.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            shape.text = String
            slide.delete(slide)

ppt.save('BODd.pptx')

After execution I am getting a memory error. No clue how to resolve this issue. How can I delete ppt slides using some specific keywords?

like image 913
Shaon Avatar asked Apr 25 '26 12:04

Shaon


1 Answers

It is possible to delete the whole slides using the following code. So just use this before generating the slides to have a clean and empty PowerPoint file. By changing the index, you can also delete the specific slides.

import os
import pptx.util
from pptx import Presentation

cwd = os.getcwd()
prs = Presentation(cwd + '\\ppt.pptx')
for i in range(len(prs.slides)-1, -1, -1): 
    rId = prs.slides._sldIdLst[i].rId
    prs.part.drop_rel(rId)
    del prs.slides._sldIdLst[i]
like image 71
HMD.F Avatar answered Apr 28 '26 00:04

HMD.F



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!