Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active October 26, 2021 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/7643745ade814e074360251852c210af to your computer and use it in GitHub Desktop.
Save aspose-com-gists/7643745ade814e074360251852c210af to your computer and use it in GitHub Desktop.
Find and Replace Text in PowerPoint PPTX/PPT using C++
// File paths
const String sourceFilePath = u"SourceDirectory\\Slides\\SamplePresentation.pptx";
const String outputFilePath = u"OutputDirectory\\FindAndReplaceText_out.pptx";
// Load the Presentation file
SharedPtr<Presentation> presentation = MakeObject<Presentation>(sourceFilePath);
String strToFind = u"Title";
String strToReplaceWith = u"Heading";
// Loop through the slides
for (SharedPtr<ISlide> slide : presentation->get_Slides())
{
// Get all text frames
System::ArrayPtr<SharedPtr<ITextFrame>> textFrame = SlideUtil::GetAllTextBoxes(slide);
for (int i = 0; i < textFrame->get_Length(); i++)
{
for (SharedPtr<IParagraph> paragraph : textFrame->idx_get(i)->get_Paragraphs())
{
for (SharedPtr<IPortion> portion : paragraph->get_Portions())
{
String str = portion->get_Text();
// Find text to be replaced
int index = str.IndexOf(strToFind);
if (index != -1)
{
// Replace exisitng text with the new text
String strStartText = str.Substring(0, index);
Console::WriteLine(str.get_Length());
Console::WriteLine(strToFind.get_Length());
Console::WriteLine(str.get_Length() - 1 - (index + strToFind.get_Length() - 1));
String strEndText = str.Substring(index + strToFind.get_Length(), 60);
portion->set_Text(strStartText + strToReplaceWith + strEndText);
}
}
}
}
}
// Save the Presentation file
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment