Inserting an Image into a Cell
Introduction
Fitting a picture exactly to a single cell is a common requirement when designing spreadsheets that act as visual reports, product catalogs, employee directories, dashboards, or inventory lists. Rather than stretching an image across many cells or placing it loosely on a worksheet, you may want a clean, cell-bound image that stays aligned with the cell that owns it.
Aspose.Cells supports this scenario in two complementary ways:
- Approach 1 — Place a floating picture over a cell. Add a
Pictureto the worksheet, set itsplacementtoMOVE_AND_SIZE, and adjust its anchor cells (upper_left_row,upper_left_column,lower_right_row,lower_right_column) so the picture covers exactly one cell. - Approach 2 — Embed an image directly in a cell. Assign image bytes to the cell’s
embedded_imageproperty. The image automatically scales to fit the cell’s display area and travels with the cell.
The rest of this article walks through both approaches, explains the relevant APIs, and shows how to use them in code.
Approach 1: Place a Picture Over a Cell
A floating picture is a Picture object that lives on the worksheet drawing layer. Although it is not part of any single cell, it is anchored to a cell range. The picture’s anchor cells — its upper-left and lower-right corners — determine its visual extent on the worksheet. By default, a freshly added picture spans several cells.
To make a floating picture cover exactly one cell, you need to:
- Add the picture using
Worksheet.pictures.add(row, column, stream), which anchors the new picture to the given cell. - Set the four anchor properties so the picture’s bounding rectangle coincides with the target cell.
- Set
Picture.placementtoPlacementType.MOVE_AND_SIZEso the picture moves and resizes with the underlying cell when the user changes the column width or row height.
Anchoring the Picture to a Single Cell
The picture’s anchor is defined by four zero-based index properties:
Picture.upper_left_row— the row index of the picture’s top edge.Picture.upper_left_column— the column index of the picture’s left edge.Picture.lower_right_row— the row index of the picture’s bottom edge. To make the picture’s bottom edge sit at the bottom of rowr, set this tor + 1.Picture.lower_right_column— the column index of the picture’s right edge. To make the picture’s right edge sit at the right of columnc, set this toc + 1.
For example, to fit the picture exactly into cell C6 (row index 5, column index 2), set upper_left_row = 5, upper_left_column = 2, lower_right_row = 6, and lower_right_column = 3.
Controlling Placement Behavior
Picture.placement is an enum of type PlacementType that controls how the picture behaves when the user resizes the row or column beneath it. The recommended value for a single-cell picture is PlacementType.MOVE_AND_SIZE, which causes the picture to move and resize together with its underlying cell, preserving the exact fit.
Step-by-Step Instructions
- Create a new
Workbook(or open an existing one). - Access the target
Worksheetfromworkbook.worksheets[0]. - Open the image file from disk into a file stream (or a
BytesIOobject) using awithblock so the stream is disposed properly. - Call
worksheet.pictures.add(5, 2, stream)to add a picture anchored to cell C6. Capture the returnedPicturereference. - Set the four anchor coordinates so the picture covers only cell C6:
upper_left_row = 5,upper_left_column = 2,lower_right_row = 6,lower_right_column = 3. - Set
picture.placement = PlacementType.MOVE_AND_SIZEto keep the picture aligned with C6 when the column or row is resized. - Optionally add sample text to surrounding cells to demonstrate that only cell C6 contains the picture.
- Save the workbook to disk as an
.xlsxfile.
The following code demonstrates the complete approach.
import aspose.cells as ac
workbook = ac.Workbook()
worksheet = workbook.worksheets[0]
with open("logo.png", "rb") as fs:
pic_index = worksheet.pictures.add(5, 2, fs)
picture = worksheet.pictures[pic_index]
picture.upper_left_row = 5
picture.upper_left_column = 2
picture.lower_right_row = 6
picture.lower_right_column = 3
picture.placement = ac.PlacementType.MOVE_AND_SIZE
workbook.save("output.xlsx", ac.SaveFormat.XLSX)
Approach 2: Embed an Image Directly in a Cell
Aspose.Cells also exposes a simpler mechanism for cell-bound images: the Cell.embedded_image property. Assigning image bytes to this property attaches the image to the cell itself, as if it were inline content.
How Embedded Images Work
- The image is stored as part of the cell content rather than as a shape on the drawing layer.
- The image automatically scales to fit inside the cell’s rendered boundaries. No anchor coordinates or placement settings are required.
- The cell remains a real cell with a real address that can be referenced by formulas, sorted as part of a row, or used in other cell-level operations.
This makes Cell.embedded_image the most concise option when your goal is simply “an image that lives inside this cell.”
Step-by-Step Instructions
- Create a new
Workbook(or open an existing one). - Access the target
Worksheetfromworkbook.worksheets[0]. - Read the image file from disk into a
bytesobject (for example, by opening the file in binary mode and calling.read()). - Get a reference to the target cell — either through
worksheet.cells["C6"]orworksheet.cells[5, 2]. - Assign the bytes object to the cell’s
embedded_imageproperty. - Optionally adjust the row height and column width of the target row and column to give the embedded image a more prominent appearance.
- Save the workbook to disk as an
.xlsxfile.
The following code demonstrates the complete approach.
import aspose.cells as ac
workbook = ac.Workbook()
worksheet = workbook.worksheets[0]
# Get the target cell C6
cell = worksheet.cells["C6"]
# Read the image file into a byte array
with open("logo.png", "rb") as f:
imageData = f.read()
# Embed the image directly into the cell
cell.embedded_image = imageData
# Optionally adjust row height and column width so the embedded image is more visible
worksheet.cells.set_column_width(2, 30) # Column C (index 2)
worksheet.cells.set_row_height(5, 100) # Row 6 (index 5)
# Save the resulting workbook as an .xlsx file
workbook.save("output.xlsx", ac.SaveFormat.XLSX)
Choosing the Right Approach
Both approaches produce a picture that fits inside a single cell, but they differ in how the picture is stored and how it behaves:
-
Use a floating picture (Approach 1) when:
- You need finer control over placement, layering, or alignment with other drawing objects.
- You want the picture to behave as a shape that can be selected, reordered, or grouped with other shapes.
- You require legacy compatibility with code that already works with
picturescollections. - You need to compute anchor coordinates dynamically based on worksheet layout.
-
Use an embedded image (Approach 2) when:
- You want the simplest possible insertion of an image into a cell.
- The image should travel with the cell like any other cell content.
- You do not need to manipulate the image as a shape.
Related Articles
- How to Insert Picture in Cell
- Add Image Hyperlinks
- Load a Web Image from a URL into an Excel Worksheet
- Manipulate Position Size and Designer Chart