Add Icons to Worksheet

Add Icons to Worksheet in Aspose.Cells for Python via .NET

If you need to use Aspose.Cells for Python via .NET to add ‘icons’ in an Excel file, then this document can provide you with some help.

The Excel interface corresponding to the insert icon operation is as follows:

  • Select the position of the icon to be inserted in the worksheet
  • Left click Insert->Icons
  • In the window that opens, select the icon in the red rectangle in the figure above
  • Left click Insert,it will be inserted into the Excel file.

The effect is as follows:

Here, we have prepared sample code to help you insert icons using Aspose.Cells for Python via .NET.There is also a necessary sample file and an icon resource file.We used the Excel interface to insert an icon with the same display effect as the resource file in the sample file.

Python Sample Code

from aspose import pycore
from aspose.cells import SaveFormat, Workbook
from aspose.pydrawing import Color
import bytearray
import int
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Read icon resource file data
fileName = "icon.svg"
fsSource = open(fileName, "rb")
bytes = bytearray(utils.filesize(fsSource))
numBytesToRead = pycore.cast(int, utils.filesize(fsSource))
numBytesRead = 0
while numBytesToRead > 0:
# Read may return anything from 0 to numBytesToRead.
n = fsSource.readinto(bytes)
# Break when the end of the file is reached.
if n == 0:
break
numBytesRead = n
numBytesToRead = n
fsSource.close()
# Create workbook from sample file
workbook = Workbook("sample.xlsx")
# Access first worksheet from the collection
sheet = workbook.worksheets[0]
# Add the icon to the worksheet
sheet.shapes.add_icons(3, 0, 7, 0, 100, 100, bytes, None)
# Set a prompt message
c = sheet.cells.get(8, 7)
c.value = "Insert via Aspose.Cells"
s = c.get_style()
s.font.color = Color.blue
c.set_style(s)
# Save.You can check your icon in this way.
workbook.save("sample2.xlsx", SaveFormat.XLSX)

When you execute the above code in your project, you will get the following results: