Install-Package FreeSpire.Presentation -Version 7.8.0
using Spire.Presentation; using Spire.Presentation.Charts; using Spire.Presentation.Converter.Equation.Word; using System; using System.IO; using System.Text; namespace ExtractText { class Program { static void Main(string[] args) { // 堆代码 duidaima.com //初始化Presentation类的实例 Presentation presentation = new Presentation(); //载PowerPoint文档示例 presentation.LoadFromFile("海市蜃楼是如何形成的?.pptx"); //创建StringBuilder实例 StringBuilder sb = new StringBuilder(); //遍历文档中的每张幻灯片 foreach (ISlide slide in presentation.Slides) { //遍历每张幻灯片中的每个形状 foreach (IShape shape in slide.Shapes) { //检查形状是否为IAutoShape类型 if (shape is IAutoShape) { //以每种形状遍历所有段落 foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs) { //提取文本并保存到StringBuilder实例 sb.AppendLine(tp.Text); } } } } //创建一个新的txt文件以保存提取的文本 File.WriteAllText("提取文本.txt", sb.ToString()); presentation.Dispose(); } } }VB.NET语言实现
Imports Spire.Presentation Imports Spire.Presentation.Charts Imports Spire.Presentation.Converter.Equation.Word Imports System.IO Imports System.Text // 堆代码 duidaima.com Namespace ExtractText Friend Class Program Private Shared Sub Main(ByVal args As String()) '初始化Presentation类的实例 Dim presentation As Presentation = New Presentation() '载PowerPoint文档示例 presentation.LoadFromFile("海市蜃楼是如何形成的?.pptx") '创建StringBuilder实例 Dim sb As StringBuilder = New StringBuilder() '遍历文档中的每张幻灯片 For Each slide As ISlide In presentation.Slides '遍历每张幻灯片中的每个形状 For Each shape As IShape In slide.Shapes '检查形状是否为IAutoShape类型 If TypeOf shape Is IAutoShape Then '以每种形状遍历所有段落 For Each tp As TextParagraph In TryCast(shape, IAutoShape).TextFrame.Paragraphs '提取文本并保存到StringBuilder实例 sb.AppendLine(tp.Text) Next End If Next Next '创建一个新的txt文件以保存提取的文本 Call File.WriteAllText("提取文本.txt", sb.ToString()) presentation.Dispose() End Sub End Class End Namespace
效果图