Python via JavaでPowerPointテキスト段落を管理する

概要

Aspose.Slides for Python via Javaは、テキストをテキストフレーム、段落、およびポーションの階層として表現します:

  • TextFrame は、シェイプ内のテキストコンテナを表し、その段落コレクションへのアクセスを提供します。
  • Paragraph は、テキストフレーム内の1つの段落を表し、ポーションと段落レベルの書式設定へのアクセスを提供します。
  • Portion は、段落内のテキストランを表します。各ポーションは独自のテキストと文字レベルの書式設定を持つことができます。

したがって、段落は複数のポーションを使用することで、フォント、色、サイズ、その他の書式設定が異なるテキストを含めることができます。

段落の作成と書式設定

シェイプのテキストフレーム内に段落を作成し、ポーションを使用して個々のテキストランの書式を設定します。このセクションでは、各段落内で�数のポーションを組み合わせ、異なる色、フォントサイズ、スタイルを適用する方法を示します。

複数のポーションを使用した段落の作成

以下のPython例は、3つの段落を作成し、各段落はParagraph.getPortions が返すコレクション内の3つのテキストランを含みます。デフォルトの段落と空のポーションを再利用し、残りの段落とポーションを追加します。書式設定はPortion.getPortionFormat を通じて適用されます。各段落の最初のポーションは赤・太字・15ポイント、2番目は青・斜体・18ポイントです。3番目のポーションはデフォルトの書式設定のままです。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, NullableBool, Paragraph, Portion, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 150, 300, 150)
    text_frame = shape.getTextFrame()

    first_paragraph = text_frame.getParagraphs().get_Item(0)
    first_paragraph.getPortions().add(Portion())
    first_paragraph.getPortions().add(Portion())

    second_paragraph = Paragraph()
    second_paragraph.getPortions().add(Portion())
    second_paragraph.getPortions().add(Portion())
    second_paragraph.getPortions().add(Portion())
    text_frame.getParagraphs().add(second_paragraph)

    third_paragraph = Paragraph()
    third_paragraph.getPortions().add(Portion())
    third_paragraph.getPortions().add(Portion())
    third_paragraph.getPortions().add(Portion())
    text_frame.getParagraphs().add(third_paragraph)

    paragraph_count = text_frame.getParagraphs().getCount()
    for paragraph_index in range(paragraph_count):
        paragraph = text_frame.getParagraphs().get_Item(paragraph_index)
        portion_count = paragraph.getPortions().getCount()
        for portion_index in range(portion_count):
            portion = paragraph.getPortions().get_Item(portion_index)
            portion.setText(f"Portion {paragraph_index + 1}.{portion_index + 1}")

            if portion_index == 0:
                portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.RED)
                portion.getPortionFormat().setFontBold(NullableBool.True_)
                portion.getPortionFormat().setFontHeight(15)
            elif portion_index == 1:
                portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE)
                portion.getPortionFormat().setFontItalic(NullableBool.True_)
                portion.getPortionFormat().setFontHeight(18)

    presentation.save("paragraphs_with_portions.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

箇条書きおよび番号付きリストの作成

段落テキストを記号、番号、またはカスタム画像を使用したリストに整理します。このセクションの例は、箇条書きの書式設定、多層リスト、カスタム開始番号をカバーします。

箇条書きまたは番号付きリストの作成

箇条書きと番号付けは、関連項目をスキャンしやすくします。Aspose.Slidesでは、リスト設定はBulletFormat を通じて定義されます。

このPython例は、同じテキストフレームに記号箇条書きと番号付き項目を追加します。BulletFormat.setType は箇条書きのタイプを選択し、BulletFormat.setChar は記号を指定し、BulletFormat.setNumberedBulletStyle は番号付けスタイルを選択します。両方の項目は高さ100%の黒い箇条書きを使用し、段落インデントは25ポイントです:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BulletType, ColorType, NullableBool, NumberedBulletStyle, Paragraph, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = shape.getTextFrame()
    text_frame.getParagraphs().clear()

    symbol_paragraph = Paragraph()
    symbol_paragraph.setText("Welcome to Aspose.Slides")
    symbol_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
    symbol_paragraph.getParagraphFormat().getBullet().setChar("•")
    symbol_paragraph.getParagraphFormat().setIndent(25)
    symbol_paragraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB)
    symbol_paragraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK)
    symbol_paragraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True_)
    symbol_paragraph.getParagraphFormat().getBullet().setHeight(100)
    text_frame.getParagraphs().add(symbol_paragraph)

    numbered_paragraph = Paragraph()
    numbered_paragraph.setText("This is a numbered item")
    numbered_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
    numbered_paragraph.getParagraphFormat().getBullet().setNumberedBulletStyle(NumberedBulletStyle.BulletCircleNumWDBlackPlain)
    numbered_paragraph.getParagraphFormat().setIndent(25)
    numbered_paragraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB)
    numbered_paragraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK)
    numbered_paragraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True_)
    numbered_paragraph.getParagraphFormat().getBullet().setHeight(100)
    text_frame.getParagraphs().add(numbered_paragraph)

    presentation.save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

画像箇条書きの使用

画像箇条書きは、記号や番号の代わりにカスタム画像を使用できます。

このPython例は、プレゼンテーションの画像コレクションにPPImage として画像を追加し、段落の箇条書きとして使用します。画像箇条書きを使用するためにBulletFormat.setType を呼び出し、画像はBulletFormat.getPicture で割り当てます。BulletFormat.setHeight を100%に設定して、段落テキストに対する箇条書きのサイズを決定します:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BulletType, Images, Paragraph, Presentation, SaveFormat, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    bullet_image = Images.fromFile("bullets.png")
    try:
        presentation_image = presentation.getImages().addImage(bullet_image)
    finally:
        bullet_image.dispose()

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = shape.getTextFrame()
    text_frame.getParagraphs().clear()

    paragraph = Paragraph()
    paragraph.setText("Welcome to Aspose.Slides")
    paragraph.getParagraphFormat().getBullet().setType(BulletType.Picture)
    paragraph.getParagraphFormat().getBullet().getPicture().setImage(presentation_image)
    paragraph.getParagraphFormat().getBullet().setHeight(100)
    text_frame.getParagraphs().add(paragraph)

    presentation.save("picture_bullet.pptx", SaveFormat.Pptx)
    presentation.save("picture_bullet.ppt", SaveFormat.Ppt)
finally:
    presentation.dispose()

多層リストの作成

ParagraphFormat.setDepth を設定して、段落をリストの異なるレベルに配置します。最上位レベルの深さは 0 です。

このPython例は、4つの段落に対してParagraphFormat.setDepth の値を0から3まで割り当て、1つのテキストフレーム内に階層を作成します。各段落は記号箇条書きを使用し、BulletFormat.setChar で丸い箇条書きとハイフンを交互に設定して隣接するレベルを区別します:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BulletType, FillType, Paragraph, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = shape.getTextFrame()
    text_frame.getParagraphs().clear()

    first_paragraph = Paragraph()
    first_paragraph.setText("Content")
    first_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
    first_paragraph.getParagraphFormat().getBullet().setChar("•")
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    first_paragraph.getParagraphFormat().setDepth(0)

    second_paragraph = Paragraph()
    second_paragraph.setText("Second level")
    second_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
    second_paragraph.getParagraphFormat().getBullet().setChar('-')
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    second_paragraph.getParagraphFormat().setDepth(1)

    third_paragraph = Paragraph()
    third_paragraph.setText("Third level")
    third_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
    third_paragraph.getParagraphFormat().getBullet().setChar("•")
    third_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    third_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    third_paragraph.getParagraphFormat().setDepth(2)

    fourth_paragraph = Paragraph()
    fourth_paragraph.setText("Fourth level")
    fourth_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
    fourth_paragraph.getParagraphFormat().getBullet().setChar('-')
    fourth_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    fourth_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    fourth_paragraph.getParagraphFormat().setDepth(3)

    text_frame.getParagraphs().add(first_paragraph)
    text_frame.getParagraphs().add(second_paragraph)
    text_frame.getParagraphs().add(third_paragraph)
    text_frame.getParagraphs().add(fourth_paragraph)

    presentation.save("multilevel_list.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

番号付きリスト項目の開始番号をカスタム値に設定

番号付き段落の最初に表示される番号を設定するには、BulletFormat.setNumberedBulletStartWith を使用します。

このPython例は、3つの段落に対して番号付けを使用するようにBulletFormat.setType を設定し、BulletFormat.setNumberedBulletStartWith にそれぞれ2、3、7 の値を割り当てます。開始値は各段落ごとに個別に設定でき、表示される番号を飛ばすことができます:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BulletType, Paragraph, Presentation, SaveFormat, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = shape.getTextFrame()
    text_frame.getParagraphs().clear()

    first_paragraph = Paragraph()
    first_paragraph.setText("Start at 2")
    first_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
    first_paragraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(2)

    text_frame.getParagraphs().add(first_paragraph)

    second_paragraph = Paragraph()
    second_paragraph.setText("Start at 3")
    second_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
    second_paragraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(3)
    text_frame.getParagraphs().add(second_paragraph)

    third_paragraph = Paragraph()
    third_paragraph.setText("Start at 7")
    third_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
    third_paragraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(7)
    text_frame.getParagraphs().add(third_paragraph)

    presentation.save("custom_numbered_list.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

段落レイアウトと終端プロパティの制御

段落の余白とインデントを調整して、先頭行と折り返し行の位置を制御します。さらに、表示テキストのポーションとは別に段落終端記号の書式設定も可能です。

先頭行インデントの設定

ParagraphFormat.setIndent メソッドを使用して段落の先頭行インデントを制御します。この設定は段落の左余白に対して先頭行のみを移動させます。正の値は先頭行を右にシフトし、残りの行は段落本文に揃ったままです。

段落全体を移動させる必要がある場合はParagraphFormat.setMarginLeft を使用します。先頭行だけを移動させる場合はParagraphFormat.setIndent を使用します。

以下の例は、3つの段落に同じ20ポイントのParagraphFormat.setMarginLeft と、0、20、40ポイントのParagraphFormat.setIndent の値を設定します。先頭行は徐々に大きなオフセットで開始し、折り返し行は同じ左余白を保持します:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, Paragraph, Presentation, SaveFormat, ShapeType, TextAutofitType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 420, 220)
    shape.getFillFormat().setFillType(FillType.NoFill)
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY)

    text_frame = shape.getTextFrame()
    text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape)
    text_frame.getParagraphs().clear()

    first_paragraph = Paragraph()
    first_paragraph.setText("No first-line indent. Wrapped lines start at the same position as the first line.")
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    first_paragraph.getParagraphFormat().setMarginLeft(20.0)
    first_paragraph.getParagraphFormat().setIndent(0.0)

    second_paragraph = Paragraph()
    second_paragraph.setText("First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body.")
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().setFillType(FillType.Solid)
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    second_paragraph.getParagraphFormat().setMarginLeft(20.0)
    second_paragraph.getParagraphFormat().setIndent(20.0)

    third_paragraph = Paragraph()
    third_paragraph.setText("First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see.")
    third_paragraph.getParagraphFormat().getDefaultPortionFormat().setFillType(FillType.Solid)
    third_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    third_paragraph.getParagraphFormat().setMarginLeft(20.0)
    third_paragraph.getParagraphFormat().setIndent(40.0)

    text_frame.getParagraphs().add(first_paragraph)
    text_frame.getParagraphs().add(second_paragraph)
    text_frame.getParagraphs().add(third_paragraph)

    presentation.save("paragraph_indent.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

結果:

段落の先頭行インデント

ぶら下げインデントの設定

ぶら下げインデントは、先頭行が残りの行の左側から始まる段落レイアウトです。Aspose.Slidesでは、ParagraphFormat.setIndent プロパティでこの効果を作成します。Indent を負の値に設定すると、段落本文に対して先頭行が左に移動します。

実際には、ParagraphFormat.setMarginLeft が段落本文の左位置を定義し、ParagraphFormat.setIndent がその余白に対する先頭行の位置を定義します。ぶら下げインデントを作成するには、正の MarginLeft 値と負の Indent 値を設定します。

この書式設定は、参考文献、引用、用語集エントリ、その他折り返し行が段落本文の下に揃う必要がある段落に便利です。

以下のPython例は、40ポイントと60ポイントのParagraphFormat.setMarginLeft の値と、-20ポイントと-30ポイントのParagraphFormat.setIndent の値を組み合わせます。各段落で、先頭行は折り返し行の左側から開始します。2番目の段落はより深いぶら下げインデントになります:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, Paragraph, Presentation, SaveFormat, ShapeType, TextAutofitType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 420, 220)
    shape.getFillFormat().setFillType(FillType.NoFill)
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY)

    text_frame = shape.getTextFrame()
    text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape)
    text_frame.getParagraphs().clear()

    first_paragraph = Paragraph()
    first_paragraph.setText("A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body.")
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    first_paragraph.getParagraphFormat().setMarginLeft(40.0)
    first_paragraph.getParagraphFormat().setIndent(-20.0)

    second_paragraph = Paragraph()
    second_paragraph.setText("This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare.")
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    second_paragraph.getParagraphFormat().setMarginLeft(60.0)
    second_paragraph.getParagraphFormat().setIndent(-30.0)

    text_frame.getParagraphs().add(first_paragraph)
    text_frame.getParagraphs().add(second_paragraph)

    presentation.save("hanging_indent.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

結果:

段落のぶら下げインデント

段落終端ランのプロパティ設定

Paragraph.setEndParagraphPortionFormat メソッドは段落終端記号の書式設定を制御します。以下の例は、PortionFormat を作成し、BasePortionFormat.setFontHeight を48ポイント、BasePortionFormat.setLatinFont をTimes New Romanに設定します。この書式を第2段落の終端記号に割り当て、表示テキストのポーションは変更しません:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FontData, Paragraph, Portion, PortionFormat, Presentation, SaveFormat, ShapeType

presentation = Presentation("Test.pptx")
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, 200, 250)
    text_frame = shape.getTextFrame()
    text_frame.getParagraphs().clear()

    first_paragraph = Paragraph()
    first_portion = Portion("Sample text")
    first_paragraph.getPortions().add(first_portion)

    second_paragraph = Paragraph()
    second_portion = Portion("Sample text 2")
    second_paragraph.getPortions().add(second_portion)

    end_paragraph_format = PortionFormat()
    end_paragraph_format.setFontHeight(48)
    latin_font = FontData("Times New Roman")
    end_paragraph_format.setLatinFont(latin_font)
    second_paragraph.setEndParagraphPortionFormat(end_paragraph_format)

    text_frame.getParagraphs().add(first_paragraph)
    text_frame.getParagraphs().add(second_paragraph)

    presentation.save("end_paragraph_format.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

レンダリングされた行数のカウント

自動折り返しや行末の句読点に影響する段落規則については、Control Line Breaking と Control Hanging Punctuation を参照してください。

Paragraph.getLinesCount を使用して、テキストレイアウト後の段落が占める行数(自動折り返しを含む)をカウントします。これはプレゼンテーションテンプレートでテキストの長さとレイアウトを確認する際に便利です。

段落はTextFrame.getParagraphs の1つの項目で、複数のレンダリング行を占めることがあります。段落内の明示的な改行は新しい行を強制しますが、別の段落は作成しません。自動折り返しは利用可能な幅に基づいて行を生成し、テキストに明示的な改行文字を挿入しません。そのため、段落や改行文字の数を数えてもレンダリングされた行数は得られません。

以下の例はテキストシェイプを作成し、行数をカウントし、シェイプを狭め、テキストを短い文字列に置き換えます。例ではTextFrameFormat.setWrapText で折り返しを有効にし、TextFrameFormat.setAutofitType で自動フィットを無効にして、シェイプの幅が折り返しを制御し、テキストやシェイプの自動縮小を行わないようにします。各測定はParagraph.getLinesCount を使用します。シェイプの寸法はポイント単位です。最後に、別の段落を追加し、テキストフレーム全体の行数を合計します。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import NullableBool, Paragraph, Presentation, ShapeType, TextAutofitType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 400, 200)
    text_frame = shape.getTextFrame()
    text_frame.getTextFrameFormat().setWrapText(NullableBool.True_)
    text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.None_)

    paragraph = text_frame.getParagraphs().get_Item(0)
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20)
    paragraph.setText("This text demonstrates how automatic wrapping changes the number of rendered lines.")
    print("Original width:", paragraph.getLinesCount())

    shape.setWidth(150)
    print("Narrower shape:", paragraph.getLinesCount())

    paragraph.setText("Short text.")
    print("Shorter text:", paragraph.getLinesCount())

    second_paragraph = Paragraph()
    second_paragraph.setText("Another paragraph.")
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20)
    text_frame.getParagraphs().add(second_paragraph)

    total_line_count = 0
    for current_paragraph in text_frame.getParagraphs():
        total_line_count += current_paragraph.getLinesCount()
    print("Total lines in the text frame:", total_line_count)
finally:
    presentation.dispose()

このテキストと寸法でシェイプを狭めると行数が増加し、短い文字列に置き換えると減少します。正確なカウントはフォントの利用可否や置換、フォントサイズ、余白、インデント、折り返し、オートフィット設定により変わります。テンプレートを確認する際は、対象環境向けのフォントとレイアウト設定を使用してください。

行数だけではテキストがコンテナからはみ出すかどうかは判断できません。利用可能な高さ、行高、段落および行間、オートフィットの動作も重要です。折り返しが無効な場合、1行だけでも利用可能な幅を超えることがあります。

段落コンテンツのインポートとエクスポート

HTMLを使用してテキストをプレゼンテーションに取り込んだり、既存の段落を外部で使用するためにエクスポートしたりします。このセクションでは、テーブルセル内の段落を含む個々の段落を画像としてレンダリングする方法も示します。

HTMLテキストを段落にインポート

ParagraphCollection.addFromHtml を使用して、HTMLマークアップをテキストフレーム内の段落とポーションに変換します。

このPython例はファイルからHTMLを読み取り、マークアップをParagraphCollection.addFromHtml に渡します。テキストフレームのデフォルト段落をクリアし、フレームにインポートされたコンテンツのみが含まれるようにします。矩形シェイプはスライドの各側に10ポイントの余白を残します:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, Presentation, SaveFormat, ShapeType
from pathlib import Path

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape_width = presentation.getSlideSize().getSize().getWidth() - 20
    shape_height = presentation.getSlideSize().getSize().getHeight() - 20

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, shape_width, shape_height)
    shape.getFillFormat().setFillType(FillType.NoFill)
    shape.getTextFrame().getParagraphs().clear()

    try:
        html = Path("file.html").read_text(encoding="utf-8")
        shape.getTextFrame().getParagraphs().addFromHtml(html)

        presentation.save("html_text.pptx", SaveFormat.Pptx)
    except OSError as exception:
        print("The HTML file could not be read: " + str(exception))
finally:
    presentation.dispose()

段落テキストをHTMLにエクスポート

ParagraphCollection.exportToHtml を使用して、選択した段落範囲をHTMLとしてエクスポートします。プレゼンテーションのテキストをウェブサイトで再利用する場合、プレゼンテーションのフォント、サイズ、テキストボックススタイルを引き継がずにローカルの強調表示を保持したいことがあります。TextToHtmlConversionOptions.setTextInheritanceLimit を設定して、エクスポート時の書式継承範囲を制御します。

サンプルプレゼンテーション をダウンロードし、アプリケーションの作業ディレクトリに配置してください。最初のスライドにはテキスト “Inherited text; explicit red 18pt” を含むテキストシェイプが1つあります。入力は意図的にいくつかの書式レベルを使用しています:

  • プレゼンテーションのデフォルトテキストスタイルはCalibriフォントと28ptのサイズを提供します。
  • テキストフレームの第一レベルテキストスタイルは青色テキストと斜体を提供します。
  • 段落は明示的に太字テキストを設定します。
  • 2番目のポーション「explicit red 18pt」は、赤色テキストと18ptのサイズを明示的に設定します。

以下の完全な例は、同じ段落を3回エクスポートします。TextToHtmlConversionOptions をエクスポートメソッドに渡し、各結果を個別のUTF-8 HTMLファイルとして書き出します:

import jpype
import asposeslides
from pathlib import Path

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import AutoShape, Presentation, TextInheritanceLimit, TextToHtmlConversionOptions

presentation = Presentation("text-inheritance.pptx")
try:
    shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0)

    if isinstance(shape, AutoShape):
        text_frame = shape.getTextFrame()
        if text_frame is not None:
            paragraphs = text_frame.getParagraphs()
            
            modes = [TextInheritanceLimit.All, TextInheritanceLimit.TextBox, TextInheritanceLimit.ParagraphOnly]
            file_names = ["All.html", "TextBox.html", "ParagraphOnly.html"]

            for mode, file_name in zip(modes, file_names):
                options = TextToHtmlConversionOptions()
                options.setTextInheritanceLimit(mode)

                html = paragraphs.exportToHtml(0, paragraphs.getCount(), options)
                try:
                    Path(file_name).write_text(str(html), encoding="utf-8")
                except OSError as exception:
                    print("The HTML file could not be written: " + str(exception))
        else:
            print("The first shape does not contain a text frame.")
    else:
        print("The first shape is not a text shape.")
finally:
    presentation.dispose()

生成されたHTMLは最初のポーション「Inherited text; 」に対する次のCSSを含みます。表は関連する宣言を一覧化しており、他の生成されたプロパティは簡潔さのために省略されています。

出力 最初のポーションの関連CSS 見た目
All.html font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold フォントが利用可能な場合、Calibriの28ptで青色、太字、斜体テキスト。
TextBox.html color:#0000ff; font-style:italic; font-weight:bold 青色、太字、斜体テキスト;フォントファミリーとサイズは周囲のページまたはブラウザが提供します。
ParagraphOnly.html font-weight:bold 太字テキスト;フォントファミリー、サイズ、色、斜体スタイルは周囲のページまたはブラウザが提供します。

2番目のポーションは3つの出力すべてで color:#ff0000、font-size:18pt、font-weight:bold を保持します。また、All と TextBox では斜体も保持されますが、ParagraphOnly では保持されません。継承を制限しても、段落やポーションに明示的に割り当てられた書式は削除されません。

同じブラウザでファイルを開いて比較してください。2つの制限モードでは、省略されたCSSプロパティは宛先ページのスタイルまたはブラウザのデフォルトで解決されるため、最終的な見た目はHTMLが使用される場所に依存します。

HTMLクリップボードフラグメントの生成

TextToHtmlConversionOptions.setAddClipboardFragmentHeader を使用して、ParagraphCollection.exportToHtml の出力形式を選択します。False の場合、メソッドは通常のHTMLを返します。True の場合、Windowsクリップボードヘッダーが先頭に付加されます。StartHTML と EndHTML フィールドはHTMLのUTF-8バイトオフセットを示し、StartFragment と EndFragment フィールドは貼り付けるコンテンツの境界を示します。

この例は、2つの書式設定された段落を通常のHTML(html)とHTMLクリップボードフラグメント(clipboard_html)としてエクスポートします。アプリケーションはフラグメントをシステムクリップボードに書き込むことができます。クリップボードAPIが生のバイトを期待する場合、clipboard_html.encode(\"utf-8\") を使用してBOMなしでフラグメントをエンコードします。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, ShapeType, TextToHtmlConversionOptions

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    
    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 0, 0, 400, 100)
    paragraphs = shape.getTextFrame().getParagraphs()
    paragraphs.clear()
    paragraphs.addFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>")

    options = TextToHtmlConversionOptions()
    options.setAddClipboardFragmentHeader(False)
    html = str(paragraphs.exportToHtml(0, paragraphs.getCount(), options))

    options.setAddClipboardFragmentHeader(True)
    clipboard_html = str(paragraphs.exportToHtml(0, paragraphs.getCount(), options))
finally:
    presentation.dispose()

段落を画像としてレンダリング

Paragraph.getImage は個々の段落を直接レンダリングし、画像オブジェクトを返します。結果は save メソッドでファイルまたはストリームに保存できます。包含するシェイプをレンダリングしたり、ビットマップを手動で切り取る必要はありません。

Paragraph.getImage は、段落が親コレクションで見つからない、レンダリング範囲が有効でない、またはレンダリングできない場合に None を返すことがあります。保存する前に結果を確認し、使用後は返された画像を破棄してください。

デフォルトスケールで段落をレンダリング

この例は、1枚のスライドを持つプレゼンテーションを使用し、最初のシェイプが3つの段落を含むテキストボックスです。

3つの段落を含むテキストボックス

以下の例はインデックス1の2番目の段落を選択し、スケールパラメータなしでParagraph.getImage を呼び出します。シェイプが必要な段落を含んでいるか、レンダリングが画像を返すかを確認した上で、save メソッドでPNG形式で保存します。finally ブロックで使用後に画像を破棄します。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import AutoShape, ImageFormat, Presentation

presentation = Presentation("sample.pptx")
try:
    shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0)

    if isinstance(shape, AutoShape):
        text_shape = shape
        text_frame = text_shape.getTextFrame()
        if text_frame is not None and text_frame.getParagraphs().getCount() > 1:
            paragraph = text_frame.getParagraphs().get_Item(1)
            paragraph_image = paragraph.getImage()

            if paragraph_image is not None:
                try:
                    paragraph_image.save("paragraph.png", ImageFormat.Png)
                finally:
                    paragraph_image.dispose()
            else:
                print("The paragraph could not be rendered.")
        else:
            print("The expected paragraph was not found.")
    else:
        print("The first shape is not a text shape.")
finally:
    presentation.dispose()

結果:

段落画像

テーブルセル内の段落をスケーリングしてレンダリング

scale_x と scale_y パラメータを受け取るParagraph.getImage のオーバーロードを使用して、水平および垂直スケール係数を設定します。以下の例はテーブルを作成し、最初のセル内の段落をデフォルト幅と高さの2倍でレンダリングし、結果をPNG画像として保存します。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import ImageFormat, Presentation

scale_x = 2.0
scale_y = 2.0
presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    table = slide.getShapes().addTable(50, 50, [300.0], [80.0])

    paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0)
    paragraph.setText("Text in a table cell")

    paragraph_image = paragraph.getImage(scale_x, scale_y)

    if paragraph_image is not None:
        try:
            paragraph_image.save("table_paragraph.png", ImageFormat.Png)
        finally:
            paragraph_image.dispose()
    else:
        print("The paragraph could not be rendered.")
finally:
    presentation.dispose()

1 のスケール係数はその軸をデフォルトのピクセルサイズに保ちます。たとえば、両方の係数を 2 にすると、幅と高さがデフォルトの約2倍となり、ピクセル数は4倍になります。大きな係数はズームや高解像度出力でテキストをより鮮明にしますが、メモリ使用量とファイルサイズも増加します。1 未満の係数は詳細が少ない小さな画像を生成します。アスペクト比を保つには同じ係数を使用し、水平と垂直で異なる係数を使用すると出力が個別に伸びます。

Shape.getImage でシェイプ全体をレンダリングすることは、出力にシェイプの塗り、枠線、その他の視覚コンテキストを含める必要がある場合に有用です。段落のみの画像の場合は、Paragraph.getImage を使用してください。

FAQ

テキストフレーム内の行折り返しを完全に無効にできますか?

はい。TextFrameFormat.setWrapText を設定して折り返しを無効にすると、行はテキストフレームの端で折り返されません。

特定の段落のスライド上での正確な境界を取得するにはどうすればよいですか?

Paragraph.getRect を使用して段落の境界矩形を取得します。Portion.getRect は個々のポーションの境界を提供します。

段落の配置(左揃え、右揃え、中央揃え、両端揃え)はどこで制御されますか?

ParagraphFormat.setAlignment は段落レベルの設定であり、個々のポーションの書式設定に関係なく、段落全体に適用されます。行内で異なるフォントサイズのポーションを垂直に揃えるには、行内でフォントを揃える を参照してください。

段落の一部に校正言語を設定できますか?

はい。個々のポーションに対してBasePortionFormat.setLanguageId を設定すれば、1つの段落内に複数の言語のテキストを含めることができます。