Find and Replace Text in PowerPoint using Python

Hi,

I am currently evaluating ASPOSE.SLIDES for Java. I saw in some of your examples the ability to find and replace text in PowerPoints, I saw the example below in Java, do you have such example in Python,

Presentation presentation = new Presentation(“test.pptx”);
String strToReplace = “Replaced”;
ITextFrame[] tb = SlideUtil.getAllTextFrames(presentation, true);
String strToFind = “test”;
for (int i = 0; i < tb.length; i++)
for (IParagraph ipParagraph : tb[i].getParagraphs())
for (IPortion iPortion : ipParagraph.getPortions())
if (iPortion.getText().contains(strToFind))

{

iPortion.setText(iPortion.getText().replace(strToFind,strToReplace));

}

presentation.save(“test.pptx”,SaveFormat.Pptx);

Many thanks,

Matt

@Knight010101,

I have observed your requirements and suggest you to please visit this documentation link to replace text via Aspose.Slides for Java in Python.

1 Like

Very helpful. Many thanks.

Hi again,

I downloaded the example from the link you shared, this worked really great after some tweaking from side. What I am actually after is the ability to Find and Replace. The example I have only set Text on targeted placeholders, not find and replace text.

Thanks,

@Knight010101,

I like to share that the documentation only offers examples in Java officially and you may please need to write similar code based on Java example on your end for your required language by referring to example. I have written example code by porting from Java to Python and you may try using it in your environment with some tweaking if required.

        presentation =  self.Presentation(self.dataDir +"test.pptx")
       
        strToReplace = "Replaced"

        textFrames = SlideUtil.getAllTextFrames(presentation, true)
        strToFind = "test"
        
        for textFrame in textFrames:
            for  ipParagraph in textFrame.getParagraphs():
                for iPortion in ipParagraph.getPortions():
                    if iPortion.getText().find(strToFind)==-1:
                        print "No 'is' here!"
                    else:
                        iPortion.setText(strToReplace)
        
        # Write the presentation as a PPTX file
        save_format = self.SaveFormat
        pres.save(self.dataDir + "Welcome_PH.pptx", save_format.Pptx)

        print "Replaced text, please check the output file."
1 Like

Ok thanks. I think I can work with this. Appreciate your time.

Hi there,

I have tried your example, done all I can with it. All I could get out when I dumped/debugged the page is this “ReadOnly Count Parent_Immediate”.

Just to add, I debugged the code on this line for _iPortion in ipParagraph.getPortions():_

Many thanks,

@Knight010101,

I have observed the issue shared by you. @Adnan.Ahmad has rightly pointed about support of Python code as one may need to port the Java code to Python on his end. I suggest you to please try using following modification in your code as the code for iterating list of portions inside paragraph seems fine.

            #for iPortion in ipParagraph.getPortions():
            for x in range(0,ipParagraph.getPortions().getCount()):
                iPortion=ipParagraph.getPortions().get_Item(x)
1 Like