java Java读取pptx文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10684526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:05:51  来源:igfitidea点击:

Java read pptx file

javapowerpoint

提问by user1290932

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 :

有人可以帮助我如何在 java 中读取 pptx 文件吗?我希望可以使用 apache POI 读取此文件,我已经搜索过本教程,但找不到它。我已使用此代码成功读取了 ppt 文件:

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?

有人能告诉我必须修改这段代码的哪一部分才能读取 pptx 文件吗?

回答by Arne de Bruijn

According to http://poi.apache.org/slideshow/index.htmlyou 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

根据http://poi.apache.org/slideshow/index.html,您需要使用一组单独的类来读取 OOXML .pptx 文件。食谱中有示例代码:http: //poi.apache.org/slideshow/xslf-cookbook.html

回答by Saurabh Shetty

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

以下链接提供了从 ppt 读取数据的完整代码... http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/DataExtraction。爪哇

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:

使用以下方法获取 ppt 中的边列表:

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.

检查形状的类型,它应该是 XSLFTextShape 或 XSLFictureShape。然后您可以相应地处理和显示数据。

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

这是进一步阅读有关形状的链接。
https://poi.apache.org/apidocs/org/apache/poi/xslf/usermodel/XSLFShape.html

The response by @Arne de Bruijn Hope this helps.

@Arne de Bruijn 希望这会有所帮助。

回答by kostas

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

根据apache poi release notes,3.8版本可以读取PPTX。不过,您必须查看文档。