这篇文章将介绍如何使用Spire.Presentation for Java提取PowerPoint文档中的文本。
源文档:
import com.spire.presentation.*;
import java.io.FileWriter;
public class ExtractText {
public static void main(String[] args) throws Exception {
//创建Presentation实例
Presentation ppt = new Presentation();
//加载PowerPoint文档
ppt.loadFromFile("Input.pptx");
StringBuilder buffer = new StringBuilder();
//遍历文档中的幻灯片,提取文本
for (Object slide : ppt.getSlides()) {
for (Object shape : ((ISlide) slide).getShapes()) {
if (shape instanceof IAutoShape) {
for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
buffer.append(((ParagraphEx) tp).getText());
}
}
}
}
//保存文本到文本文件
FileWriter writer = new FileWriter("ExtractText.txt");
writer.write(buffer.toString());
writer.flush();
writer.close();
}
}
输出: