Managing Pictures
Aspose.Cells for Python via .NET allows developers to add pictures to spreadsheets at runtime. Moreover, the positioning of these pictures can be controlled at runtime, which is discussed in more detail in the coming sections.
This article explains how to add pictures, and how to insert an image that shows the content of certain cells.
Adding Pictures
Adding pictures to a spreadsheet is very easy. It only takes a few lines of code: Simply call the add method of the pictures collection (encapsulated in the Worksheet object). The add method takes the following parameters:
- Upper left row index, the index of the upper left row.
- Upper left column index, the index of the upper left column.
- Image file name, the name of the image file, complete with path.
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") |
Positioning Pictures
There are two possible ways to control the positioning of pictures using Aspose.Cells for Python via .NET:
- Proportional positioning: define a position proportional to the row height and width.
- Absolute positioning: define the exact position on the page where the image will be inserted, for example, 40 pixels to the left and 20pixels below the edge of the cell.
Proportional Positioning
Developers can position the pictures proportional to row height and column width using the upper_delta_x and upper_delta_y properties of the Aspose.Cells.Drawing.Picture object. A Picture object can be obtained from the pictures collection by passing its picture index. This example places an image in the F6 cell.
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") |
Absolute Positioning
Developers can also position the pictures absolutely by using the left and top properties of the Picture object. This example places an image in cell F6, 60 pixels from the left and 10 pixels from the top of the cell.
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") |
Inserting a Picture Based on Cell Reference
Aspose.Cells for Python via .NET lets you display the contents of a worksheet cell in an image shape. You can link the picture to the cell that contains the data that you want to display. Since the cell, or cell range, is linked to the graphic object, changes that you make to the data in that cell or cell range automatically appear in the graphic object.
Add a picture to the worksheet by calling the add_picture method of the ShapeCollection collection (encapsulated in the Worksheet object). Specify the cell range by using the formula attribute of the Picture object.
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") |