LoginSignup
0
0

More than 1 year has passed since last update.

Java PowerPointでテキストを取得

Posted at

今回はSpire.Presentation for Javaという無料のライブラリを使用して、PowerPointでテキストを取得する方法を紹介します。

 下準備

1.E-iceblueの公式サイトからFree Spire. Presentation for Java無料版をダウンロードしてください。

f:id:lendoris:20210526155656p:plain
2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. Presentation.jarを参照に追加してください。

f:id:lendoris:20210526155715p:plain

元のファイル

f:id:lendoris:20210526155741p:plain

import com.spire.presentation.*;

import java.io.FileWriter;

public class ExtractText {
    public static void main(String[] args) throws Exception {

        //Presentation objectを作成します。
        Presentation ppt = new Presentation();
        //PowerPointファイルをロードしmなす。
        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();
    }
}

実行結果

f:id:lendoris:20210526155830p:plain

 

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0