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

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

Aspose.Slides FOSS for Python provides fine-grained text formatting through the PortionFormat クラス。A Portion テキストの最小の独立単位です。段落内の単一の書式ランにマッピングされます。このガイドでは、プレゼンテーションのテキストに太字、斜体、フォントサイズ、カラー書式を適用する方法を示します。.

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

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

pip install aspose-slides-foss

ステップ 2: テキストフレームを持つシェイプを追加する

テキストをフォーマットする前に、シェイプは a を含んでいる必要があります。 TextFrame.。 Use 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

A TextFrame はリストを含んでいます Paragraph オブジェクト (tf.paragraphs). 各 Paragraph 含む Portion オブジェクト (paragraph.portions).


ステップ 4: 太字と斜体のフォーマットを適用する

使用 portion_format.font_boldportion_format.font_italic. これらのプロパティは受け入れます NullableBool.TRUE, NullableBool.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つの段落に複数の Portion を使用する

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

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.TRUE vs True

portion_format.font_bold 期待する NullableBool.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

参照

 日本語