Introduction

This guide shows how to create Excel charts from Python data using Aspose.Cells FOSS — a pure-Python, MIT-licensed library for building .xlsx workbooks without Microsoft Office. You will learn how to build column, line, and pie charts with named data series, category labels, and legend control.

Automated reporting pipelines often need charts embedded in Excel workbooks, such as quarterly summaries, dashboards, or operational metrics. Aspose.Cells FOSS provides a chart API that uses one method per chart type (add_bar, add_line, add_pie) and keyword arguments for series configuration.


Quick Start

Run the following command to install the aspose-cells-foss package from PyPI:

pip install aspose-cells-foss

Import the Workbook and Cell classes at the top of your script:

from aspose.cells_foss import Workbook, Cell

Charts are accessed via ws.charts; no separate chart module import is required.


The Chart API at a Glance

What you wantHow to do it
Add a column chartws.charts.add_bar(top_row, left_col, bottom_row, right_col)
Add a line chartws.charts.add_line(top_row, left_col, bottom_row, right_col)
Add a bar chartws.charts.add_bar(top_row, left_col, bottom_row, right_col)
Add a pie chartws.charts.add_pie(top_row, left_col, bottom_row, right_col)
Set the titlechart.title = "My Chart" (plain string; not .title.text)
Set x-axis categorieschart.category_data = "A2:A6"
Add a named serieschart.n_series.add("B2:B6", category_data="A2:A6", name="Revenue")
Show legendchart.show_legend = True
Set legend positionchart.legend_position = "bottom"

All row and column indices are zero-based. The add_* methods return the Chart object directly (not an index).


Column Chart: Step by Step

Let’s build a column chart showing quarterly revenue and expenses.

Step 1: Create the workbook and write data

Create a Workbook, select the first worksheet, and write header and data rows to cells:

from aspose.cells_foss import Workbook, Cell

wb = Workbook()
ws = wb.worksheets[0]
ws.name = "Q1-Q4 Report"

##Headers
ws.cells["A1"].value = "Quarter"
ws.cells["B1"].value = "Revenue"
ws.cells["C1"].value = "Expenses"

##Data
quarters = ["Q1", "Q2", "Q3", "Q4"]
revenue  = [50000, 62000, 71000, 89000]
expenses = [32000, 38000, 41000, 47000]

for i, (q, r, e) in enumerate(zip(quarters, revenue, expenses), start=2):
    ws.cells[f"A{i}"].value = q
    ws.cells[f"B{i}"].value = r
    ws.cells[f"C{i}"].value = e

Step 2: Add the chart

Position the chart below the data (row 7 onward, spanning columns 0-8):

chart = ws.charts.add_bar(6, 0, 22, 8)

Step 3: Configure title and legend

Set the chart title and legend position using plain string assignment:

chart.title = "Quarterly Revenue vs Expenses"
chart.show_legend = True
chart.legend_position = "bottom"

Step 4: Add data series

Each series needs a value range, category range, and a name:

chart.category_data = "A2:A5"
chart.n_series.add("B2:B5", category_data="A2:A5", name="Revenue")
chart.n_series.add("C2:C5", category_data="A2:A5", name="Expenses")

Step 5: Save

Call Workbook.save() with a filename ending in .xlsx to write the workbook to disk:

wb.save("quarterly_report.xlsx")
print("Chart saved to quarterly_report.xlsx")

The following block assembles all five steps into a single runnable script:

from aspose.cells_foss import Workbook, Cell

wb = Workbook()
ws = wb.worksheets[0]
ws.name = "Q1-Q4 Report"

##Data
ws.cells["A1"].value = "Quarter"
ws.cells["B1"].value = "Revenue"
ws.cells["C1"].value = "Expenses"

data = [("Q1", 50000, 32000), ("Q2", 62000, 38000),
        ("Q3", 71000, 41000), ("Q4", 89000, 47000)]
for i, (q, r, e) in enumerate(data, start=2):
    ws.cells[f"A{i}"].value = q
    ws.cells[f"B{i}"].value = r
    ws.cells[f"C{i}"].value = e

chart = ws.charts.add_bar(6, 0, 22, 8)
chart.title = "Quarterly Revenue vs Expenses"
chart.category_data = "A2:A5"
chart.n_series.add("B2:B5", category_data="A2:A5", name="Revenue")
chart.n_series.add("C2:C5", category_data="A2:A5", name="Expenses")
chart.show_legend = True
chart.legend_position = "bottom"

wb.save("quarterly_report.xlsx")

Line Chart: Monthly Trend

The same pattern used for column charts applies to add_line() — replace add_bar() with add_line() to produce a line chart instead:

from aspose.cells_foss import Workbook, Cell

wb = Workbook()
ws = wb.worksheets[0]

months  = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
actuals = [42000, 47500, 53000, 49000, 61000, 68000]
targets = [45000, 45000, 50000, 55000, 60000, 65000]

ws.cells["A1"].value = "Month"
ws.cells["B1"].value = "Actual"
ws.cells["C1"].value = "Target"

for i, (m, a, t) in enumerate(zip(months, actuals, targets), start=2):
    ws.cells[f"A{i}"].value = m
    ws.cells[f"B{i}"].value = a
    ws.cells[f"C{i}"].value = t

chart = ws.charts.add_line(8, 0, 24, 8)
chart.title = "Monthly Revenue: Actual vs Target"
chart.category_data = "A2:A7"
chart.n_series.add("B2:B7", category_data="A2:A7", name="Actual")
chart.n_series.add("C2:C7", category_data="A2:A7", name="Target")
chart.show_legend = True
chart.legend_position = "right"

wb.save("monthly_trend.xlsx")

Pie Chart: Market Share

The following example creates a pie chart with market share data. Call add_pie() and configure category and series ranges the same way as other chart types:

from aspose.cells_foss import Workbook, Cell

wb = Workbook()
ws = wb.worksheets[0]

segments = ["Product A", "Product B", "Product C", "Product D"]
shares   = [40, 25, 20, 15]

ws.cells["A1"].value = "Segment"
ws.cells["B1"].value = "Share %"
for i, (s, v) in enumerate(zip(segments, shares), start=2):
    ws.cells[f"A{i}"].value = s
    ws.cells[f"B{i}"].value = v

chart = ws.charts.add_pie(6, 0, 20, 7)
chart.title = "Market Share by Product"
chart.category_data = "A2:A5"
chart.n_series.add("B2:B5", category_data="A2:A5", name="Share")
chart.show_legend = True
chart.legend_position = "right"

wb.save("market_share.xlsx")

Common Mistakes to Avoid

WrongRight
ws.charts.add(ChartType.COLUMN, 5, 0, 15, 5)ws.charts.add_bar(5, 0, 15, 5)
chart.title.text = "Revenue"chart.title = "Revenue"
chart.n_series.add("B2:B5", True) (positional)chart.n_series.add("B2:B5", category_data="A2:A5", name="Revenue")
from aspose.cells.charts import ChartTypeNot needed; use ws.charts.add_bar() directly
chart.n_series.category_data = "A2:A5"chart.category_data = "A2:A5" (chart level)

Getting Started

You now have working examples for column, line, and pie charts in Aspose.Cells FOSS for Python. The chart API follows a consistent pattern: call the add_* method, set the title and legend, add named series with n_series.add(), and call Workbook.save().

Resources