有时需要从PowerPoint幻灯片中提取文本以执行文本分析。另一方面,可能需要提取文本并将其保存在文件或数据库中以进行进一步处理。因此,本文介绍了如何使用C#从PowerPoint演示文稿中提取文本。特别是,将学习如何从特定的幻灯片或整个演示文稿中提取文本。
- 从PowerPoint幻灯片中提取文本
- 从PowerPoint演示文稿中提取文本
为了处理PowerPoint演示文稿,Aspose提供Aspose.Slides for .NET旨在在.NET应用程序中实现PowerPoint自动化功能。它还提供了一些简单的方法来从PPTX演示文稿中提取文本。
>>你可以获取下载Aspose.Slides 最新版测试体验。
从C#中的PowerPoint幻灯片中提取文本
以下是从PowerPoint演示文稿中的幻灯片中提取文本的步骤。
- 使用Presentation类加载演示文稿。
- 使用SlideUtil.GetAllTextBoxes()方法将一张幻灯片中的所有文本框获取到ITextFrame数组中。
- 遍历每个ITextFrame并使用ITextFrame.Paragraphs属性访问其文本。
- 从段落的每个章节中检索并打印文本。
下面的代码示例演示如何使用C#从PowerPoint幻灯片中提取文本。
//Instatiate PresentationEx class that represents a PPTX file
Presentation pptxPresentation = new Presentation("demo.pptx");
//Get an Array of TextFrameEx objects from the first slide
ITextFrame[] textFramesSlideOne = SlideUtil.GetAllTextBoxes(pptxPresentation.Slides[0]);
//Loop through the Array of TextFrames
for (int i = 0; i < textFramesSlideOne.Length; i++)
//Loop through paragraphs in current TextFrame
foreach (Paragraph para in textFramesSlideOne[i].Paragraphs)
//Loop through portions in the current Paragraph
foreach (Portion port in para.Portions)
{
//Display text in the current portion
Console.WriteLine(port.Text);
//Display font height of the text
Console.WriteLine(port.PortionFormat.FontHeight);
//Display font name of the text
Console.WriteLine(port.PortionFormat.LatinFont.FontName);
}
}
}
从C#中的PowerPoint演示文稿中提取文本
还可以使用Aspose.Slides for .NET从整个PowerPoint演示文稿中提取文本。以下是执行此操作的步骤。
- 使用Presentation类加载演示文稿。
- 使用SlideUtil.GetAllTextFrames()方法获取演示文稿中的所有文本框架。
- 遍历每个ITextFrame并访问其Paragraphs。
- 访问段落的各个部分并打印其文本。
下面的代码示例演示如何从PowerPoint演示文稿中提取文本。
//Instatiate Presentation class that represents a PPTX file
Presentation pptxPresentation = new Presentation("demo.pptx");
//Get an Array of ITextFrame objects from all slides in the PPTX
ITextFrame[] textFramesPPTX = Aspose.Slides.Util.SlideUtil.GetAllTextFrames(pptxPresentation, true);
//Loop through the Array of TextFrames
for (int i = 0; i < textFramesPPTX.Length; i++)
//Loop through paragraphs in current ITextFrame
foreach (IParagraph para in textFramesPPTX[i].Paragraphs)
//Loop through portions in the current IParagraph
foreach (IPortion port in para.Portions)
{
//Display text in the current portion
Console.WriteLine(port.Text);
//Display font height of the text
Console.WriteLine(port.PortionFormat.FontHeight);
//Display font name of the text
if (port.PortionFormat.LatinFont != null)
Console.WriteLine(port.PortionFormat.LatinFont.FontName);
}
}
}
如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。