Introduction

Aspose.Cells enables developers to programmatically create and customize charts directly within spreadsheets using Python. The library empowers users to visualize data without relying on external tools or manual intervention in Microsoft Excel. This capability is essential for generating dynamic reports, dashboards, and data-driven documents that require immediate insight from raw spreadsheet data.

With Aspose.Cells, you can add various chart types (including column, line, pie, and area charts) to a worksheet and define data sources using the n_series collection. These features make it straightforward to transform static spreadsheet data into engaging visual representations.

import os
from aspose.cells_foss import Workbook, ChartType

def get_output_directory():
    return os.path.abspath(os.path.join(".", "..", "..", "..", "Data", "02_OutputDirectory"))

def run_how_to_create_custom_chart():
    # Create a new workbook and get the first worksheet
    workbook = Workbook()
    worksheet = workbook.worksheets[0]

    # Populate cells with sample data
    worksheet.cells.get_cell_by_name("A1").put_value(50)
    worksheet.cells.get_cell_by_name("A2").put_value(100)
    worksheet.cells.get_cell_by_name("A3").put_value(150)
    worksheet.cells.get_cell_by_name("A4").put_value(110)
    worksheet.cells.get_cell_by_name("B1").put_value(260)
    worksheet.cells.get_cell_by_name("B2").put_value(12)
    worksheet.cells.get_cell_by_name("B3").put_value(50)
    worksheet.cells.get_cell_by_name("B4").put_value(100)

    # Add a bar chart to the worksheet using the dedicated add_bar() method
    chart = worksheet.charts.add_bar(5, 0, 25, 10)

    # Add NSeries data source ranging from A1 to B4
    chart.n_series.add("A1:B4", True)

    # Save the workbook
    output_path = os.path.join(get_output_directory(), "outputHowToCreateCustomChart.xlsx")
    workbook.save(output_path)

    print("HowToCreateCustomChart executed successfully.")

if __name__ == "__main__":
    run_how_to_create_custom_chart()

Key Highlights

Aspose.Cells empowers developers to generate professional-quality charts directly within spreadsheets using Python. With intuitive APIs, users can define chart types, bind data ranges, and set chart titles, all programmatically. This capability is especially valuable for report automation, data analysis dashboards, and business intelligence workflows where consistent, embeddable visualizations are essential.

  • Support for multiple chart types including column, line, bar, and pie charts via dedicated add_bar(), add_line(), add_pie(), and add_area() methods.
  • Direct binding of worksheet cell ranges as data sources using n_series.add() with optional category_data and name keyword arguments.
  • Programmatic control over chart titles via chart.title = "My Title" (plain string assignment).
  • Category axis configuration via chart.category_data = "A2:A6".
  • Legend control via chart.show_legend and chart.legend_position.
  • Charts of type LINE, BAR, PIE, AREA, and STOCK serialize fully to XLSX; other chart types raise NotImplementedError on save.

Getting Started

Aspose.Cells enables developers to programmatically create and customize charts within spreadsheets using Python. With minimal code, you can populate worksheet cells, add a chart, define its data source, and apply formatting, all without requiring Microsoft Excel.

from aspose.cells_foss import Workbook, ChartType

# Create a workbook and access the first worksheet
workbook = Workbook()
worksheet = workbook.worksheets[0]

# Populate cells with sample data
worksheet.cells["A1"].value = "Q1"
worksheet.cells["B1"].value = 42
worksheet.cells["A2"].value = "Q2"
worksheet.cells["B2"].value = 78

# Add a bar chart and bind data
chart = worksheet.charts.add_bar(5, 0, 20, 8)
chart.n_series.add("A1:B2", True)

# Save the workbook
workbook.save("chart_output.xlsx")

See Also