Add picture in worksheet

How can add picture in a worksheet and specify the cell where it needs to be using Aspose.Cells for Python via .NET.

Suppose the cells is B432 and I have the picture path say “C:\input\img.jpg”

@shiv12345,

See the following sample code to accomplish your task for your reference:
e.g.
Sample code:

import aspose.cells
from aspose.cells import Workbook

# Open an existing Excel file.
wb = Workbook("Book1.xlsx")
#  Access first worksheet.
ws = wb.worksheets[0]
#  Add the picture to B432 cell.
idx = ws.pictures.add(431, 1, "c:\\input\\img.jpg")
#  Save the workbook in output XLSX format.
wb.save("out1.xlsx") 

Hope, this helps a bit.

1 Like

yes this was extremly helpful, further how can I set height and width of the image and also i want to uncheck “Lock aspect ratio”

Also, Is there any documentation for “Aspose.Cells for Python via .NET”, I have seen the documentation for Aspose.Cells for .NET but not the same for Python, can you direct me toward it if it exists?

@shiv12345,

You can set height, width and lock aspect ratio attributes for the inserted picture. See the following updated sample code for your reference.
e.g.
Sample code:

import aspose.cells
from aspose.cells import Workbook

# Open an existing Excel file.
wb = Workbook("Book1.xlsx")
#  Access first worksheet.
ws = wb.worksheets[0]
#  Add the picture to B432 cell.
idx = ws.pictures.add(431, 1, "c:\\input\\img.jpg")
# Accessing the newly added picture.
picture = ws.pictures[idx]

# Set height, width, lock aspect ratio and other attributes of the picture.
picture.left = 60
picture.top = 10
picture.width = 100
picture.height = 50
picture.is_lock_aspect_ratio = True

#  Save the workbook in output XLSX format.
wb.save("out1.xlsx") 

Also, check the Picture API Reference page for your reference.
https://reference.aspose.com/cells/python-net/aspose.cells.drawing/picture/

There is no document in Python for it at the moment. But you may see the .NET document on managing pictures and easily convert the code to Python accordingly.

And, we will also check and add the relevant document in Aspose.Cells for Python via .NET Docs soon.

1 Like