JavaScript のプレゼンテーションから高度なテキスト抽出
スライドからテキストを抽出
Aspose.Slides for Node.js via Java は SlideUtil クラスを提供します。このクラスは、プレゼンテーションまたはスライドからテキスト全体を抽出するための多数のオーバーロードされた静的メソッドを公開しています。PPTX プレゼンテーションのスライドからテキストを抽出するには、SlideUtil クラスが提供するオーバーロードされた静的メソッド getAllTextBoxes を使用します。このメソッドは Slide オブジェクトをパラメータとして受け取ります。実行すると、Slide メソッドはパラメータとして渡されたスライドからテキスト全体を走査し、TextFrame オブジェクトの配列を返します。これにより、テキストに関連付けられたすべての書式情報を取得できます。以下のコードは、プレゼンテーションの最初のスライド上のすべてのテキストを抽出します:
// PPTX ファイルを表す Presentation クラスをインスタンス化
var pres = new aspose.slides.Presentation("demo.pptx");
try {
for (var s = 0; s < pres.getSlides().size(); s++) {
let slide = pres.getSlides().get_Item(s);
// PPTX のすべてのスライドから ITextFrame オブジェクトの配列を取得
var textFramesPPTX = aspose.slides.SlideUtil.getAllTextBoxes(slide);
// TextFrame の配列をループ
for (var i = 0; i < textFramesPPTX.length; i++) {
// 現在の ITextFrame の段落をループ
for (let j = 0; j < textFramesPPTX[i].getParagraphs().getCount(); j++) {
let para = textFramesPPTX[i].getParagraphs().get_Item(j);
// 現在の IParagraph の部分文字列(ポーション)をループ
for (let k = 0; k < para.getPortions().getCount(); k++) {
let port = para.getPortions().get_Item(k);
// 現在のポーションのテキストを表示
console.log(port.getText());
// テキストのフォント高さを表示
console.log(port.getPortionFormat().getFontHeight());
// テキストのフォント名を表示
if (port.getPortionFormat().getLatinFont() != null) {
console.log(port.getPortionFormat().getLatinFont().getFontName());
}
});
}
}
});
} finally {
pres.dispose();
}
プレゼンテーションからテキストを抽出
プレゼンテーション全体からテキストを走査するには、SlideUtil クラスが提供する静的メソッド getAllTextFrames を使用します。このメソッドは 2 つのパラメータを受け取ります。
- 最初に、テキストを抽出する対象のプレゼンテーションを表す Presentation オブジェクト。
- 次に、テキストを走査する際にマスタースライドを含めるかどうかを示すブール値。 このメソッドは、テキスト書式情報を含む TextFrame オブジェクトの配列を返します。以下のコードは、マスタースライドを含むプレゼンテーションからテキストと書式情報を走査します。
// PPTX ファイルを表す Presentation クラスをインスタンス化
var pres = new aspose.slides.Presentation("demo.pptx");
try {
// PPTX のすべてのスライドから ITextFrame オブジェクトの配列を取得
var textFramesPPTX = aspose.slides.SlideUtil.getAllTextFrames(pres, true);
// TextFrame の配列をループ
for (var i = 0; i < textFramesPPTX.length; i++) {
// 現在の ITextFrame の段落をループ
for (let j = 0; j < textFramesPPTX[i].getParagraphs().getCount(); j++) {
let para = textFramesPPTX[i].getParagraphs().get_Item(j);
// 現在の IParagraph の部分文字列をループ
for (let k = 0; k < para.getPortions().getCount(); k++) {
let port = para.getPortions().get_Item(k);
// 現在のポーションのテキストを表示
console.log(port.getText());
// テキストのフォント高さを表示
console.log(port.getPortionFormat().getFontHeight());
// テキストのフォント名を表示
if (port.getPortionFormat().getLatinFont() != null) {
console.log(port.getPortionFormat().getLatinFont().getFontName());
}
}
}
}
} finally {
pres.dispose();
}
分類された高速テキスト抽出
Presentation クラスに新しい静的メソッド getPresentationText が追加されました。このメソッドには 3 つのオーバーロードがあります:
IPresentationText getPresentationText(String file, int mode);
IPresentationText getPresentationText(InputStream stream, int mode);
IPresentationText getPresentationText(InputStream stream, int mode, ILoadOptions options);
The TextExtractionArrangingMode enum argument indicates the mode to organize the output of text result and can be set to the following values:
- Unarranged - The raw text with no respect to position on the slide
- Arranged - The text is positioned in the same order as on the slide
Unarranged mode can be used when speed is critical, it’s faster than Arranged mode.
PresentationText represents the raw text extracted from the presentation. It contains a getSlidesText method which returns an array of SlideText objects. Every object represent the text on the corresponding slide. SlideText object have the following methods:
SlideText.getText- The text on the slide’s shapesSlideText.getMasterText- The text on the master page’s shapes for this slideSlideText.getLayoutText- The text on the layout page’s shapes for this slideSlideText.getNotesText- The text on the notes page’s shapes for this slide
There is also a SlideText class which implements the SlideText class.
The new API can be used like this:
var text1 = aspose.slides.PresentationFactory.getInstance().getPresentationText("presentation.pptx", aspose.slides.TextExtractionArrangingMode.Unarranged);
console.log(text1.getSlidesText()[0].getText());
console.log(text1.getSlidesText()[0].getLayoutText());
console.log(text1.getSlidesText()[0].getMasterText());
console.log(text1.getSlidesText()[0].getNotesText());
よくある質問
テキスト抽出時に Aspose.Slides は大規模なプレゼンテーションをどれくらい高速に処理できますか?
Aspose.Slides は高性能に最適化されており、大規模なプレゼンテーションでも効率的に処理できるため、リアルタイムまたはバルク処理のシナリオに適しています。
Aspose.Slides はプレゼンテーション内の表やチャートからテキストを抽出できますか?
はい、Aspose.Slides は表、チャート、その他の複雑なスライド要素からのテキスト抽出を完全にサポートしており、すべてのテキストコンテンツに簡単にアクセスし分析できます。
プレゼンテーションからテキストを抽出するために特別な Aspose.Slides ライセンスが必要ですか?
Aspose.Slides の無料トライアル版でもテキストを抽出できますが、スライド数に制限があるなどいくつかの制約があります。制限なく使用し、より大きなプレゼンテーションを処理するには、フルライセンスの購入が推奨されます。