Python を使用して PPTX でテキストに取り消し線を引く方法

このトピックでは、Python を使用して PPTX 内のテキストに取り消し線を引く方法に焦点を当てます。これには、環境を確立するための詳細と必要なリソース、段階的な手順、Python を使用して PPTX テキストに取り消し線を引くための実際のコード例が含まれています。サンプル プレゼンテーションの生成、オートシェイプの挿入、テキストの挿入によるテキスト フレームの追加、テキストの取り消しなどのプロセスの詳細が示されています。

Python を使用して PPTX のテキストに取り消し線を付ける手順

  1. PPTX でテキストに取り消し線を付けるための環境を確立してください。.NET経由でAspose.Slides for Pythonを使用する
  2. Presentation クラスのインスタンスを使用してデフォルトの空のプレゼンテーションを生成し、その最初のスライドにアクセスします
  3. オートシェイプを作成し、その中にサンプル テキストフレームを挿入します。
  4. テキストフレーム内にテキストの一部を挿入し、TextStrikethroughType 列挙子を使用してその部分のテキストに二重線の取り消し線を設定します。
  5. テキストフレーム内にテキストの 2 番目の部分を挿入し、TextStrikethroughType 列挙子を使用してその部分のテキストに単一の取り消し線を設定します。
  6. 取り消し線付きのテキストを含む PPTX プレゼンテーションをディスクに保存します

上記の手順では、Python を使用してプレゼンテーション内のテキストに取り消し線を引く方法を示し、目的の出力を取得するために必要なすべてのクラス、メソッド、プロパティの詳細を公開します。 Presentation クラスは空の PPTX ファイルを作成するか既存の PPTX ファイルを読み込むために使用され、ShapeCollection クラスは PPTX スライドにオートシェイプを追加するために使用され、TextStrikethroughType 列挙子は選択された部分に必要なテキストの取り消し線の種類を設定するために使用されます。

Python を使用して PPTX のテキストに取り消し線を付けるコード

import aspose.pydrawing as draw
import aspose.slides as slides
# Path to the license file directory
filepath = "Y://Documents//KnowledgeBase//TestData//"
#Load the license in your application for creating a strikethrough text
slidesTextLicense = slides.License()
slidesTextLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic")
# Instantiate the Presentation object to strikethrough text
with slides.Presentation() as presentationText:
#Access the first default slide
slide = presentationText.slides[0]
#Add an autoshape of the Rectangle type
autoShape = slide.shapes.add_auto_ahape(slides.ShapeType.Rectangle, 50, 150, 300, 0)
#Filling the shape with no fill color
autoShape.fill_format.fill_type = slides.FillType.NoFill
#Add the text frame inside the autoshape
textFrame = autoShape.add_text_frame("This is sample strikethrough text")
#Set the textual properties on the portion
portionFormat = textFrame.paragraphs[0].portions[0].portion_format
portionFormat.fill_format.fill_type = slides.FillType.Solid
portionFormat.fill_format.solid_fill_color.dolor = draw.Color.red
#Strikethrouh with double line
portionFormat.strikethrough_type = slides.TextStrikethroughType.Double;
#Add a second line of text
secondPortion = slides.Portion("Second text line ")
textFrame.Paragraphs[0].Portions.Add(secondPortion)
portionFormat = secondPortion.PortionFormat
portionFormat.fill_format.fill_type = slides.FillType.Solid
portionFormat.fill_format.solid_fill_color.color = draw.Color.blue
#Strikethrouh with a single line
portionFormat.strikethrough_type = slides.TextStrikethroughType.Single
#Save the presentation with strikethrough text on the disk
presentationText.save(filepath + "StrikethroughText.pptx", slides.export.SaveFormat.Pptx)
print("Done")

上記のコード例は、Python* を使用して PPT のテキストに *取り消し線を付けます。 Presentation クラスのインスタンスを使用してデフォルトの空のプレゼンテーションを生成し、Presentation.Slides プロパティを使用してプレゼンテーション スライド コレクション内の最初のスライドにアクセスします。オートシェイプが挿入され、続いてその中にテキストの一部が挿入されます。最後に、TextStrikethroughType 列挙子の助けを借りて、プレゼンテーション内で目的のテキストが取り消され、ディスクに保存されます。

このチュートリアルでは、Python を使用してプレゼンテーション テキストに取り消し線を引く方法を学習しました。プレゼンテーション内で ISlide を結合する方法について知りたい場合は、Python を使用して PowerPoint ファイルを結合する方法 の記事を参照してください。

 日本語