Convert Sparkline to Image and HTML in Aspose.Cells for Python via .NET

Introduction

Sparklines are a compact way to visualize trends directly inside a worksheet. While Excel users see them in place, many real-world scenarios require a sparkline to leave the cell — for example, to be embedded into a different cell as a static picture, attached to an automated email, or rendered as part of an HTML report published to the web. Aspose.Cells supports both of these operations. The sparkline.to_image method renders an individual sparkline to a stream, and the resulting bytes can be assigned to cell.embedded_image so the picture is stored inside a single cell of the workbook. Separately, HtmlSaveOptions lets you convert the entire workbook — sparklines and all — into a self-contained HTML file. This article walks through both workflows end to end.

Workflow 1 — Render Sparklines to Images and Embed Them into Cells

In this workflow you will build a worksheet that contains a small range of source values, attach three different sparkline groups (Line, Column, and Stacked/Win-Loss) to that range, render each group as a PNG, and write those PNG bytes into adjacent cells as embedded images. The final result is a single .xlsx file that contains both the live sparklines and their rendered picture counterparts.

Step-by-Step Instructions

  1. Define a working directory and ensure it exists on disk.
  2. Create a new Workbook and obtain a reference to the first Worksheet.
  3. Populate cells A1 through E1 with five sample numeric values (for example, daily sales or temperature readings).
  4. Add three SparklineGroup objects to the worksheet by calling worksheet.sparkline_groups.add(...):
    • A SparklineType.LINE group anchored at F1, with data range A1:E1.
    • A SparklineType.COLUMN group anchored at G1, with data range A1:E1.
    • A SparklineType.STACKED (win/loss) group anchored at H1, with data range A1:E1.
  5. Build an ImageOrPrintOptions instance and set its image_type to ImageType.PNG so each sparkline is rendered as a transparent PNG.
  6. Save the workbook as output_with_sparklines.xlsx.
import aspose.cells as ac
# Create a new workbook and access the first worksheet
workbook = ac.Workbook()
worksheet = workbook.worksheets[0]
# Populate sample data in cells A1:E1
worksheet.cells["A1"].put_value(5)
worksheet.cells["B1"].put_value(-3)
worksheet.cells["C1"].put_value(8)
worksheet.cells["D1"].put_value(-2)
worksheet.cells["E1"].put_value(6)
# Add a Line sparkline group anchored at F1 (column 5, row 0)
line_area = ac.CellArea()
line_area.start_column = 5
line_area.end_column = 5
line_area.start_row = 0
line_area.end_row = 0
line_idx = worksheet.sparkline_groups.add(ac.SparklineType.LINE, "A1:E1", False, line_area)
# Add a Column sparkline group anchored at G1 (column 6, row 0)
column_area = ac.CellArea()
column_area.start_column = 6
column_area.end_column = 6
column_area.start_row = 0
column_area.end_row = 0
column_idx = worksheet.sparkline_groups.add(ac.SparklineType.COLUMN, "A1:E1", False, column_area)
# Add a Win/Loss (Stacked) sparkline group anchored at H1 (column 7, row 0)
stacked_area = ac.CellArea()
stacked_area.start_column = 7
stacked_area.end_column = 7
stacked_area.start_row = 0
stacked_area.end_row = 0
stacked_idx = worksheet.sparkline_groups.add(ac.SparklineType.STACKED, "A1:E1", False, stacked_area)
# Configure image options for PNG output
image_options = ac.ImageOrPrintOptions()
image_options.image_type = ac.ImageType.PNG
# Convert the Line sparkline to image and embed it in cell F2
line_sp = worksheet.sparkline_groups[line_idx].sparklines[0]
ms = ac.MemoryStream()
line_sp.to_image(ms, image_options)
worksheet.cells["F2"].embedded_image = ms.to_array()
# Convert the Column sparkline to image and embed it in cell G2
column_sp = worksheet.sparkline_groups[column_idx].sparklines[0]
ms = ac.MemoryStream()
column_sp.to_image(ms, image_options)
worksheet.cells["G2"].embedded_image = ms.to_array()
# Convert the Win/Loss sparkline to image and embed it in cell H2
stacked_sp = worksheet.sparkline_groups[stacked_idx].sparklines[0]
ms = ac.MemoryStream()
stacked_sp.to_image(ms, image_options)
worksheet.cells["H2"].embedded_image = ms.to_array()
# Save the workbook to disk
workbook.save("output_with_sparklines.xlsx")

The code above produces a workbook where each visual representation of a sparkline is duplicated in two forms: the live, native sparkline anchored at row 1, and a static PNG picture embedded directly into a neighboring cell on row 2. Because the pictures live inside the file itself, the workbook remains a single self-contained artifact that can be emailed or archived without breaking the embedded image references. Render each sparkline group as a PNG, convert the BytesIO stream to a bytes object, and assign the bytes to the embedded_image property of the target cell — the assignment is what makes the picture part of the cell’s stored contents.