有時需要從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),我們很高興爲您提供查詢和諮詢。