JavaでPowerPointドキュメントからテキストとイメージを抽出する方法


テキストや画像は、プレゼンテーションのスライドの主な要素です.この記事では、無料の尖塔を使用してPowerPointドキュメントからテキストとイメージを抽出する方法を示します.Java用プレゼンテーション.
インストール
Mavenを使用する場合は、フリースパイアーの依存性を指定する必要があります.プロジェクトのPOMでのJavaライブラリのプレゼンテーション.XMLファイル.
<repositories>  
        <repository>  
            <id>com.e-iceblue</id>  
            <name>e-iceblue</name>  
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>  
        </repository>  
</repositories>  
<dependencies>  
    <dependency>  
        <groupId>e-iceblue</groupId>  
        <artifactId>spire.presentation.free</artifactId>  
        <version>3.9.0</version>  
    </dependency>  
</dependencies>
非Mavenプロジェクトについては、Free Spire.Presentation for Javaをダウンロードし、パッケージを解凍し、尖塔を追加します.プレゼンテーション.プロジェクトにlibフォルダ内のjarファイルを依存関係として.
import com.spire.presentation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;

public class Test {
    public static void main(String[] args) throws Exception {
        //Create a presentation instance.
        Presentation ppt = new Presentation();

        // Load the document from disk.
        ppt.loadFromFile("Sample.pptx");

            StringBuilder buffer = new StringBuilder();
            //Traverse the presentation slides to extract the text.
            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());
                        }
                    }
                }
            }
            //Save to document to .txt
            FileWriter writer = new FileWriter("ExtractText.txt");
            writer.write(buffer.toString());
            writer.flush();
            writer.close();

        //Extract all the images from the presentation slides
        for (int i = 0; i < ppt.getImages().getCount(); i++) {
            BufferedImage image = ppt.getImages().get(i).getImage();
            ImageIO.write(image, "PNG", new File(String.format("extractImage-%1$s.png", i)));
        }
    }
}
抽出された画像の効果的なスクリーンショット