Pythonでテキストをフォーマットする方法

Pythonでテキストをフォーマットする方法

Aspose.Slides FOSS for Python は、PortionFormat クラスを通じて細かいテキスト書式設定を提供します。Portion はテキストの最小の独立単位であり、段落内の単一の書式ランに対応します。このガイドでは、プレゼンテーション内のテキストに太字、斜体、フォントサイズ、カラーの書式設定を適用する方法を示します。

ステップバイステップ ガイド

ステップ 1: パッケージをインストール

pip install aspose-slides-foss

ステップ 2: テキストフレーム付きのシェイプを追加

テキストをフォーマットする前に、シェイプはTextFrameを含んでいる必要があります。shape.add_text_frame()を使用して作成してください。

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
    tf = shape.add_text_frame("Default text: will be formatted")
    prs.save("output.pptx", SaveFormat.PPTX)

ステップ 3: TextFrame にアクセスする

shape.add_text_frame()TextFrame オブジェクトを返します。shape.text_frame を使用して後で取得することもできます。

tf = shape.text_frame          # if the frame already exists
tf = shape.add_text_frame("") # creates a new frame

TextFrameParagraph オブジェクトのリスト(tf.paragraphs)を含みます。各 ParagraphPortion オブジェクト(paragraph.portions)を含みます。


ステップ 4: 太字と斜体の書式設定を適用

portion_format.font_boldportion_format.font_italic を使用してください。これらのプロパティは NullableBool.TRUENullableBool.FALSE、または NullableBool.NOT_DEFINED を受け入れます(マスターから継承)。

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
    tf = shape.add_text_frame("Bold and italic text")

    fmt = tf.paragraphs[0].portions[0].portion_format
    fmt.font_bold = NullableBool.TRUE
    fmt.font_italic = NullableBool.TRUE

    prs.save("bold-italic.pptx", SaveFormat.PPTX)

ステップ5: フォントサイズと色を設定

サイズ(ポイント)には portion_format.font_height を設定し、色には fill_format を使用します。

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool, FillType
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
    tf = shape.add_text_frame("Large corporate-blue heading")

    fmt = tf.paragraphs[0].portions[0].portion_format
    fmt.font_height = 32                          # 32pt font
    fmt.font_bold = NullableBool.TRUE
    fmt.fill_format.fill_type = FillType.SOLID
    fmt.fill_format.solid_fill_color.color = Color.from_argb(255, 0, 70, 127)

    prs.save("colored-text.pptx", SaveFormat.PPTX)

Color.from_argb(alpha, red, green, blue) は各チャンネルに対して 0–255 の値を受け入れます。


ステップ 6: 1つの段落に複数の部分

単一の段落には、異なる書式設定を持つ複数の部分を含めることができます。段落の portions コレクションに新しい Portion を追加します:

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool, FillType
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 600, 100)
    tf = shape.add_text_frame("")  # start with empty frame

    paragraph = tf.paragraphs[0]

    # First portion: normal text
    portion1 = paragraph.portions[0]
    portion1.text = "Normal text followed by "
    portion1.portion_format.font_height = 20

    # Second portion: bold red text
    portion2 = slides.Portion()
    portion2.text = "bold red text"
    portion2.portion_format.font_height = 20
    portion2.portion_format.font_bold = NullableBool.TRUE
    portion2.portion_format.fill_format.fill_type = FillType.SOLID
    portion2.portion_format.fill_format.solid_fill_color.color = Color.from_argb(255, 200, 0, 0)
    paragraph.portions.add(portion2)

    prs.save("mixed-format.pptx", SaveFormat.PPTX)

一般的な問題と対処法

色を設定してもテキストが黒のままです

色を割り当てる前に fill_format.fill_type = FillType.SOLID が設定されていることを確認してください。塗りつぶしタイプを設定しないと、色の変更が効果を持たない可能性があります。

NullableBool.TRUETrue

portion_format.font_boldNullableBool.TRUE を期待し、Python の True ではありません。Python の True を代入すると、TypeError が発生するか、バインディングに応じて何もせずに黙っている場合があります。

保存されたファイルにフォントが表示されません

latin_font プロパティはラテンフォントファミリーを設定します。設定されていない場合、プレゼンテーションテーマのフォントが使用されます。カスタムフォントは埋め込むか、閲覧マシンにインストールされている必要があります。


よくある質問

フォントファミリーを変更するにはどうすればよいですか?

設定 portion_format.latin_font:

fmt.latin_font = slides.FontData("Arial")

FontData はフォントファミリ名を文字列として受け取ります。

段落の配置を設定するにはどうすればよいですか?

paragraph_format.alignment を使用:

from aspose.slides_foss import TextAlignment

tf.paragraphs[0].paragraph_format.alignment = TextAlignment.CENTER

サポートされている値: LEFT, CENTER, RIGHT, JUSTIFY

行間の設定方法は?

paragraph_format.space_before(段落の前にポイント)またはparagraph_format.space_after(段落の後にポイント)を使用してください:

tf.paragraphs[0].paragraph_format.space_before = 12   # 12pt before
tf.paragraphs[0].paragraph_format.space_after = 6     # 6pt after

参照

 日本語