Insert a Picture Based on Cell Reference

Inserting a Picture Based on Cell Reference

Aspose.Cells for Python via .NET supports displaying 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.

Code Example

from aspose import pycore
from aspose.cells import ConditionalFormattingIcon, IconSetType, Workbook
from aspose.cells.drawing import Picture
from io import BytesIO
# 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")
# Get the conditional icon's image data
imagedata = ConditionalFormattingIcon.get_icon_image_data(IconSetType.TRAFFIC_LIGHTS31, 0)
# Create a stream based on the image data
stream = BytesIO(imagedata)
# Add a blank picture to the D1 cell
pic = pycore.cast(Picture, workbook.worksheets[0].shapes.add_picture(0, 3, stream, 10, 10))
# 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 + "referencedpicture.out.xlsx")