Java read pptx file

∥☆過路亽.° 提交于 2019-12-11 04:45:34

问题


Can someone help me how to read pptx file in java?i would prefer if this can be read with apache POI, i have been searched this tutorial but i can't find it.I've been successfully read the ppt file with this code :

try {
    FileInputStream fis = new FileInputStream(file);
    fs = new POIFSFileSystem(fis);
    HSLFSlideShow show = new HSLFSlideShow(fs);
    SlideShow ss = new SlideShow(show);
    Slide[] slides=ss.getSlides();
    for (int x = 0; x < slides.length; x++) {
        System.out.println("Slide = " + (x + 1) + " :" + slides[x].getTitle());

        TextRun[] runs = slides[x].getTextRuns();
        for (int i = 0; i < runs.length; i++) {
            TextRun run = runs[i];
            if (run.getRunType() == TextHeaderAtom.TITLE_TYPE) {
                System.out.println("Slide title " + (i + 1) + ": " + run.getText());
            } else {
                System.out.println("Slide text run " + (i + 1) + ": "  + run.getRunType() + " : " + run.getText());
            }
        }
    }
} catch (IOException ioe) {
    ioe.printStackTrace();
}

Can someone tell me what part of this code must be modified to read pptx file?


回答1:


According to http://poi.apache.org/slideshow/index.html you need to use a separate set of classes to read OOXML .pptx files. There's example code in the cookbook: http://poi.apache.org/slideshow/xslf-cookbook.html




回答2:


Following link gives the complete code to read data from ppt... http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/DataExtraction.java

To provide a brief summary I can note the steps: First create the slide object using

XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("url of your ppt"));

Get List of Sides in your ppt using:

final List<XSLFSlide> slide = ppt.getSlides();

Iterate through the slides and get a list of shapes in each slide using:

List<XSLFShape> shapes = selectedslide.getShapes(); 

Check the type of shape, it should be either XSLFTextShape or XSLFPictureShape. Then you can process and display the data accordingly.

Here is a link for further reading about shapes.
https://poi.apache.org/apidocs/org/apache/poi/xslf/usermodel/XSLFShape.html

The response by @Arne de Bruijn Hope this helps.




回答3:


According to apache poi release notes, version 3.8 can be used to read PPTX. You have to check out the documentation though.



来源:https://stackoverflow.com/questions/10684526/java-read-pptx-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!