Skip to content

Instantly share code, notes, and snippets.

@aspose-com-kb
Last active June 15, 2024 14:03
Show Gist options
  • Save aspose-com-kb/1b98b0ca9f127a586f2cd19a01f19b80 to your computer and use it in GitHub Desktop.
Save aspose-com-kb/1b98b0ca9f127a586f2cd19a01f19b80 to your computer and use it in GitHub Desktop.
Add Sparklines in Excel using Python. For more information: https://kb.aspose.com/cells/python/add-sparklines-in-excel-using-python/
import jpype
import asposecells as cells
jpype.startJVM()
from asposecells.api import License, Workbook, CellArea, SparklineType, Color
# Instantiate a license
license = License()
license.setLicense("License.lic")
# Create a Workbook object
wb = Workbook()
sheet = wb.getWorksheets().get(0)
sheet.getCells().get("A2").putValue(8)
sheet.getCells().get("B2").putValue(3)
sheet.getCells().get("C2").putValue(5)
sheet.getCells().get("D2").putValue(4)
# Create the cell area
ca = CellArea()
ca.StartRow = 1
ca.StartColumn = 4
ca.EndRow = 1
ca.EndColumn = 4
# Add a group
idx = sheet.getSparklineGroups().add(SparklineType.LINE, sheet.getName() + "!A2:D2", False, ca);
sparklineGroup = sheet.getSparklineGroups().get(idx)
# Add a spark line
sparklineGroup.getSparklines().add(sheet.getName() + "!A2:D2", 1, 4)
# Set the color
cellsColor = wb.createCellsColor()
cellsColor.setColor(Color.getGreen())
sparklineGroup.setSeriesColor(cellsColor)
# Saving the Excel file
wb.save("output.xlsx")
print("Spark lines added successfully")
# Shutdown the JVM
jpype.shutdownJVM()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment