この例では、Python を使用して PowerPoint テーブルに画像を挿入する方法に焦点を当てています。環境を設定するために必要なすべての情報と、Python で PPTX テーブルに画像を追加するための実用的なコード例について説明しています。このアプリケーションは、Linux、macOS、Windows などの Python 構成環境で利用できます。
Python を使用して PowerPoint テーブルに画像を挿入する手順
- .NET 経由で Aspose.Slides for Python を使用 に環境を確立して、テーブル内に画像を挿入します
- Presentation クラスのインスタンスを使用してデフォルトのプレゼンテーションを作成し、スライド コレクションの最初のスライドにアクセスします
- add_table() メソッドを使用して、選択したスライド内に事前定義された数の行と列を含む表を挿入します
- プレゼンテーション画像コレクション内に画像を挿入します
- テーブル内の最初の行と列に属するセルにアクセスし、その中に画像を設定します
- テーブル イメージを含む PPTX プレゼンテーションをディスクに保存します。
上記の手順を使用して、Python で PPTX テーブルに画像を表示する方法を調べました。最初に、Presentation クラスのインスタンスを使用してデフォルトのプレゼンテーションが作成され、プレゼンテーション スライド コレクション内の最初のスライドにアクセスします。 add_table() メソッドを使用して定義済みの行数と列数を使用して新しいテーブルを作成します。次に、ディスクからソース画像をロードし、プレゼンテーション画像コレクション内に挿入します。最終的に、プレゼンテーションをディスクに保存する前に、表のセル内に追加された画像を設定します。
Python を使用して PowerPoint テーブルに画像を挿入するコード
import aspose.pydrawing as draw | |
import aspose.slides as slides | |
#Path to the license and image file directory | |
filepath = "Y://Documents//KnowledgeBase//TestData//" | |
# Load the license in your application for creating a table with an image | |
slidesTableLicense = slides.License() | |
slidesTableLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic") | |
#Create a Presentation object to add a table with an image | |
with slides.Presentation() as presTable: | |
# Access the first slide to add the table | |
slideForTable = presTable.slides[0] | |
#Define the rows' heights and columns' widths | |
dblColWidth = [50, 50, 50] | |
dblRowHeight = [50, 30, 32, 30] | |
#Add a table shape to slide | |
tblwithImage = slideForTable.shapes.add_table(100, 50, dblColWidth, dblRowHeight) | |
with open(filepath + "sample.png", "rb") as bin_file: | |
#Read the entire file from the disk at once | |
tblImageData = bin_file.read() | |
#Insert the image insdie the images collection of the presentation | |
imageForSlide = presTable.images.add_image(tblImageData) | |
#Access the first cells inside the first row of the table | |
tableCell = tblwithImage[0,0] | |
#Set the cell fill format to picture | |
tableCell.cell_format.fill_format.fill_type = slides.FillType.PICTURE | |
#Set the picture fill mode | |
tableCell.cell_format.fill_format.picture_fill_format.picture_fill_mode = slides.PictureFillMode.STRETCH | |
#Set the image for the selected cell inside the table | |
tableCell.cell_format.fill_format.picture_fill_format.picture.image = imageForSlide | |
#Save the presentations with table image | |
presTable.save(filepath + "PresWithTableImage.pptx", slides.export.SaveFormat.PPTX) | |
print("Done") | |
上記の例は、非常に単純な API インターフェイスを使用して、Python を使用してスライドにテーブル イメージを挿入する方法を示しています。 cell_format は、fill_type.PICTURE 列挙子を使用してセルの塗りつぶし形式を画像に設定するために使用されます。この例では、表のセルに PNG 画像を追加しました。ただし、表のセルに JPEG、BMP、EMF、SVG などの他の画像タイプを追加することもできます。
このトピックでは、Python を使用してプレゼンテーションにテーブル イメージを挿入する方法 について説明しました。 PowerPoint 内の表の管理について詳しく知りたい場合は、Python を使用して PowerPoint で表を作成する方法 のトピックを参照してください。