Python を使用して PowerPoint で表を作成する方法

この簡単な記事では、Python を使用して PowerPoint で表を作成する方法について説明しています。環境をセットアップするためのすべての情報、テーブル内にデータを挿入して入力するための段階的な手順、およびPython を使用してスライドにテーブルを挿入する方法を示す実際のサンプル コードについて説明します。また、テーブル セル内のテキストをフォーマットし、プレゼンテーションを PPT または PPTX 形式でディスクに保存する方法についても説明します。

Python を使用して PowerPoint で表を作成する手順

  1. アプリケーションで .NET 経由で Aspose.Slides for Python を使用するための環境 を構成して、テーブルを追加します
  2. Presentation クラスを使用して新しい空のプレゼンテーションを生成し、最初のデフォルト スライドにアクセスします
  3. add_table() メソッドを使用して、定義された列の幅と行の高さを持つ表をスライドに挿入します
  4. 新しく追加されたテーブル内の各行とそれぞれのセルをトラバースします
  5. 各セル内のフォント関連のプロパティと共にサンプル テキストを設定します。
  6. 表を含むプレゼンテーションを PPTX 形式でディスクに保存します

上記の手順では、Python を使用して PowerPoint で表を作成する方法 を説明しています。最初のステップでは、デフォルトのプレゼンテーションを作成し、その最初のスライドにアクセスします。以降の手順では、列の幅と行の高さと共にテーブルの位置座標を指定して、テーブルを追加します。最後の手順では、結果のプレゼンテーションをディスクに保存する前に、テーブル内のすべてのセルを反復処理して、それぞれの書式設定と共にテキストを設定します。

Python を使用して PowerPoint にテーブルを追加するコード

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 the table
slidesTableLicense = slides.License()
slidesTableLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic")
# Instantiate the Presentation object to add the table
with slides.Presentation() as presentationTable:
# Access the first default slide
slide = presentationTable.slides[0]
# Define the columns widths and rows heights
dblColsWidth = [50, 50, 50]
dblRowsHeight = [50, 30, 30, 30, 30]
# Insert the table shape to slide
table = slide.shapes.add_table(100, 50, dblColsWidth, dblRowsHeight)
# Set the border format for each cell
for rowIndex in range(len(table.rows)):
for cellIndex in range(len(table.rows[rowIndex])):
table.rows[rowIndex][cellIndex].cell_format.border_top.fill_format.fill_type = slides.FillType.SOLID
table.rows[rowIndex][cellIndex].cell_format.border_top.fill_format.solid_fill_color.color = draw.Color.red
table.rows[rowIndex][cellIndex].cell_format.border_top.width = 5
table.rows[rowIndex][cellIndex].cell_format.border_bottom.fill_format.fill_type = slides.FillType.SOLID
table.rows[rowIndex][cellIndex].cell_format.border_bottom.fill_format.solid_fill_color.color= draw.Color.red
table.rows[rowIndex][cellIndex].cell_format.border_bottom.width =5
table.rows[rowIndex][cellIndex].cell_format.border_left.fill_format.fill_type = slides.FillType.SOLID
table.rows[rowIndex][cellIndex].cell_format.border_left.fill_format.solid_fill_color.color =draw.Color.red
table.rows[rowIndex][cellIndex].cell_format.border_left.width = 5
table.rows[rowIndex][cellIndex].cell_format.border_right.fill_format.fill_type = slides.FillType.SOLID
table.rows[rowIndex][cellIndex].cell_format.border_right.fill_format.solid_fill_col or.color = draw.Color.red
table.rows[rowIndex][cellIndex].cell_format.border_right.width = 5
# Merge the cells 1 and 2 of row 1
table.merge_cells(table.rows[0][0], table.rows[1][1], False)
# Add the text inside the merged cell
table.rows[0][0].text_frame.text = "Merged Table Cells"
presentationTable.save(filepath + "NewPresentationWithTable.pptx", slides.export.SaveFormat.PPTX)
print("Done")

この例では、Python を使用して PowerPoint でテーブルを作成する方法 が含まれます。ここでは、Table クラス インスタンスを使用して、行と列のコレクションを持つテーブルを挿入します。 TextFrame クラス オブジェクトは、段落テキストのテキスト、フォントの高さ、および箇条書きの種類を設定します。テキストの強調表示、塗りつぶし形式の設定、フィールドの追加または削除、強調表示の色の設定など、その他の関連プロパティを使用することもできます。

このトピックでは、Python を使用してプレゼンテーションに表を挿入する方法 について説明しました。透かし画像を追加してプレゼンテーションを保護する方法に興味がある場合は、Pythonを使用してPPTXに画像の透かしを追加する方法 の記事を参照してください。

 日本語