画像の管理
Aspose.Cells for Python via .NETを使用すると、開発者はスプレッドシートに画像を実行時に追加できます。さらに、これらの画像の位置を実行時に制御できることについて詳細に解説します。
この記事では、画像の追加方法と特定のセルの内容を示す画像の挿入方法について説明します。
画像の追加
スプレッドシートに写真を追加するのは非常に簡単です。わずかなコード行だけで済みます: 単純に、picturesコレクション(Worksheetオブジェクトでカプセル化)のaddメソッドを呼び出します。addメソッドには以下のパラメータが必要です:
- 左上の行インデックス、左上の行のインデックス。
- 左上の列インデックス、左上の列のインデックス。
- 画像ファイル名、パスを含む画像ファイルの名前。
from aspose.cells import Workbook | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding a picture at the location of a cell whose row and column indices | |
# Are 5 in the worksheet. It is "F6" cell | |
worksheet.pictures.add(5, 5, dataDir + "logo.jpg") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
写真の位置合わせ
Aspose.Cells for Python via .NETを使って画像の位置を制御する方法は二つあります:
- 比例位置合わせ:行の高さと幅に比例した位置を定義します。
- 絶対位置合わせ:ページ上の画像の挿入位置を正確に定義します。例:セルの左から40ピクセル、上から20ピクセル。
比例位置合わせ
開発者は、Aspose.Cells.Drawing.Pictureオブジェクトのupper_delta_xおよびupper_delta_yプロパティを使用して、行の高さと列の幅に比例した位置に写真を配置できます。picturesコレクションからその写真のインデックスを渡すことでPictureオブジェクトを取得できます。この例では、F6セルに画像を配置します。
from aspose.cells import Workbook | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding a picture at the location of a cell whose row and column indices | |
# Are 5 in the worksheet. It is "F6" cell | |
pictureIndex = worksheet.pictures.add(5, 5, dataDir + "logo.jpg") | |
# Accessing the newly added picture | |
picture = worksheet.pictures[pictureIndex] | |
# Positioning the picture proportional to row height and colum width | |
picture.upper_delta_x = 200 | |
picture.upper_delta_y = 200 | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
絶対位置づけ
開発者は、Pictureオブジェクトのleftおよびtopプロパティを使用して、絶対位置に写真を配置することもできます。この例では、セルF6に画像を配置し、左から60ピクセル、上から10ピクセルに配置します。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding a picture at the location of a cell whose row and column indices | |
# Are 5 in the worksheet. It is "F6" cell | |
pictureIndex = worksheet.pictures.add(5, 5, dataDir + "logo.jpg") | |
# Accessing the newly added picture | |
picture = worksheet.pictures[pictureIndex] | |
# Absolute positioning of the picture in unit of pixels | |
picture.left = 60 | |
picture.top = 10 | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
セル参照に基づいて画像を挿入
Aspose.Cells for Python via .NETは、ワークシートセルの内容を画像の形に表示でき、表示したいデータが含まれるセルにリンクさせることができます。セルまたはセル範囲をグラフィックオブジェクトにリンクさせると、そのセルまたはセル範囲のデータの変更が自動的にグラフィックオブジェクトに反映されます。
Worksheetオブジェクトでカプセル化されたShapeCollectionコレクションのadd_pictureメソッドを呼び出すことで、ワークシートに画像を追加します。Pictureオブジェクトのformula属性を使用してセル範囲を指定します。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiate a new Workbook | |
workbook = Workbook() | |
# Get the first worksheet's cells collection | |
cells = workbook.worksheets[0].cells | |
# Add string values to the cells | |
cells.get("A1").put_value("A1") | |
cells.get("C10").put_value("C10") | |
# Add a blank picture to the D1 cell | |
pic = workbook.worksheets[0].shapes.add_picture(0, 3, 10, 6, None) | |
# Specify the formula that refers to the source range of cells | |
pic.formula = "A1:C10" | |
# Update the shapes selected value in the worksheet | |
workbook.worksheets[0].shapes.update_selected_value() | |
# Save the Excel file. | |
workbook.save(dataDir + "output.out.xls") |