我正在制作一个Java程序来查找文档中特定关键字的出现。我想阅读许多类型的文件格式,包括所有Microsoft Office文档。
除PowerPoint以外,我已经使用所有这些工具实现了,我正在使用StackOverflow或其他来源上的Apache POI代码段。 我发现所有幻灯片都是由形状(XSLFTextShape)制成的,但是许多幻灯片都是XSLFGraphicFrame或XSLFTable类的对象,我不能简单地使用toString()方法。如何使用Java提取其中包含的所有文本。 这是一段代码\伪代码:
File f = new File("C:\\Users\\Windows\\Desktop\\Modulo 9.pptx");
PrintStream out = System.out;
FileInputStream is = new FileInputStream(f);
XMLSlideShow ppt = new XMLSlideShow(is);
for (XSLFSlide slide : ppt.getSlides()) {
for (XSLFShape shape : slide) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape txShape = (XSLFTextShape) shape;
out.println(txShape.getText());
} else if (shape instanceof XSLFPictureShape) {
//do nothing
} else if (shape instanceof XSLFGraphicFrame or XSLFTable ) {
//print all text in it or in its children
}
}
}
答案 0 :(得分:1)
如果您的要求“在文档中查找特定关键字的出现”仅需要搜索SlideShows
的所有文本内容,则可以简单地使用SlideShowExtractor。这也可以用作POITextExtractor的入口,以获取文档元数据/属性的文本内容,例如作者和标题。
示例:
import java.io.FileInputStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.extractor.SlideShowExtractor;
import org.apache.poi.extractor.POITextExtractor;
public class SlideShowExtractorExample {
public static void main(String[] args) throws Exception {
SlideShow<XSLFShape,XSLFTextParagraph> slideshow
= new XMLSlideShow(new FileInputStream("Performance_Out.pptx"));
SlideShowExtractor<XSLFShape,XSLFTextParagraph> slideShowExtractor
= new SlideShowExtractor<XSLFShape,XSLFTextParagraph>(slideshow);
slideShowExtractor.setCommentsByDefault(true);
slideShowExtractor.setMasterByDefault(true);
slideShowExtractor.setNotesByDefault(true);
String allTextContentInSlideShow = slideShowExtractor.getText();
System.out.println(allTextContentInSlideShow);
System.out.println("===========================================================================");
POITextExtractor textExtractor = slideShowExtractor.getMetadataTextExtractor();
String metaData = textExtractor.getText();
System.out.println(metaData);
}
}
当然,有些XSLFGraphicFrame
不会被SlideShowExtractor
读取,因为apache poi
到目前为止还不支持它们。例如,各种SmartArt graphic。这些内容的文本内容存储在/ppt/diagrams/data*.xml
文档中,这些文档从幻灯片中引用。由于apache poi
到目前为止尚不支持此功能,因此只能使用底层底层方法读取。
例如,要从所有/ ppt / diagrams / data中获取所有文本,这些文本是SmartArt
图形中的文本,我们可以这样做:
...
System.out.println("===========================================================================");
//additionally get all text out of all /ppt/diagrams/data which are texts in SmartArt graphics:
StringBuilder sb = new StringBuilder();
for (XSLFSlide slide : ((XMLSlideShow)slideshow).getSlides()) {
for (org.apache.poi.ooxml.POIXMLDocumentPart part : slide.getRelations()) {
if (part.getPackagePart().getPartName().getName().startsWith("/ppt/diagrams/data")) {
org.apache.xmlbeans.XmlObject xmlObject = org.apache.xmlbeans.XmlObject.Factory.parse(part.getPackagePart().getInputStream());
org.apache.xmlbeans.XmlCursor cursor = xmlObject.newCursor();
while(cursor.hasNextToken()) {
if (cursor.isText()) {
sb.append(cursor.getTextValue() + "\r\n");
}
cursor.toNextToken();
}
sb.append(slide.getSlideNumber() + "\r\n\r\n");
}
}
}
String allTextContentInDiagrams = sb.toString();
System.out.println(allTextContentInDiagrams);
...