使用 C++ 为 PowerPoint 文件提取文本

您可能会发现自己必须将 PowerPoint 文件的文本保存到数据库或某个文件中。手动执行此任务将非常耗时且效率较低。更好的方法是以编程方式进行提取。为此,本文将教您如何使用 C++ 从 PowerPoint 文件中提取文本。

从 PowerPoint 文件中提取文本的 C++ API

Aspose.Slides for C++ 是一个本地 C++ 库,可帮助您自动执行与 PowerPoint 相关的任务。 API 支持创建、阅读和修改 PowerPoint 演示文稿。此外,该 API 还为您提供了从 PowerPoint 文件中提取文本的能力。您可以通过 NuGet 安装 API,也可以直接从 下载 部分下载。

PM> Install-Package Aspose.Slides.Cpp

使用 C++ 从 PowerPoint 幻灯片中提取文本

以下是从 PowerPoint 幻灯片中提取文本的步骤。

以下是使用 C++ 从 PowerPoint 幻灯片中提取文本的示例代码。

// 示例文件路径
const String sourceFilePath = u"SourceDirectory\\SamplePresentation.pptx";

// 加载演示文件
SharedPtr<Presentation> presentation = MakeObject<Presentation>(sourceFilePath);

// 从第一张幻灯片中获取一个 ITextFrame 对象数组
System::ArrayPtr<SharedPtr<ITextFrame>> textFramesSlideOne = SlideUtil::GetAllTextBoxes(presentation->get_Slides()->idx_get(0));

// 循环遍历 TextFrames 数组
for (int i = 0; i < textFramesSlideOne->get_Length(); i++)
{
	// 循环遍历当前 ITextFrame 中的段落
	for (SharedPtr<IParagraph> paragraph : textFramesSlideOne[i]->get_Paragraphs())
	{
		// 循环遍历当前 IParagraph 中的部分
		for (SharedPtr<IPortion> portion : paragraph->get_Portions())
		{
			// 显示文字
			Console::WriteLine(portion->get_Text());
		}
	}
}

使用 C++ 从 PowerPoint 演示文稿中提取文本

以下是从 PowerPoint 演示文稿中提取文本的步骤。

以下是使用 C++ 从 PowerPoint 演示文稿中提取文本的示例代码。

// 示例文件路径
const String sourceFilePath = u"SourceDirectory\\SamplePresentation.pptx";

// 加载演示文件
SharedPtr<Presentation> presentation = MakeObject<Presentation>(sourceFilePath);

// 从演示文稿中的所有幻灯片中获取一组 ITextFrame 对象
System::ArrayPtr<SharedPtr<ITextFrame>> textFramesPresentation = SlideUtil::GetAllTextFrames(presentation, true);

// 循环遍历 TextFrames 数组
for (int i = 0; i < textFramesPresentation->get_Length(); i++)
{
	// 循环遍历当前 ITextFrame 中的段落
	for (SharedPtr<IParagraph> paragraph : textFramesPresentation[i]->get_Paragraphs())
	{
		// 循环遍历当前 IParagraph 中的部分
		for (SharedPtr<IPortion> portion : paragraph->get_Portions())
		{
			// 显示文字
			Console::WriteLine(portion->get_Text());
		}
	}
}

获得免费许可证

您可以申请 免费的临时许可证 试用 API,而不受评估限制。

结论

在本文中,您学习了如何使用 C++ 从 PowerPoint 文件中提取文本。具体来说,您已经学习了如何从特定的 PowerPoint 幻灯片或整个 PowerPoint 演示文稿中提取文本。 Aspose.Slides for C++ 提供了一系列用于处理演示文稿的附加功能。您可以通过访问 官方文档 来详细探索 API。如有任何问题,请随时在 免费支持论坛 上与我们联系。

也可以看看