Visualization of tasks on a timeline helps manage projects better. In this article, you will learn how to create a Gantt chart in Excel using Python with Aspose.Cells for Python via .NET. With just a few lines of code, you can turn simple task data into a dynamic and clean visual timeline, ideal for reports, dashboards, and planning documents. Let’s walk through it step by step.

This article covers the following topics:

Python Excel Library for Gantt Charts

Aspose.Cells for Python via .NET is a powerful library for working with Excel files in Python. It allows you to create, modify, and format spreadsheets programmatically—including charts like Gantt charts.

Key Features:

  • Full control over Excel charts and data.
  • Support for XLSX, XLS, CSV, and PDF.
  • High performance with large files.
  • Rich chart customization options.

To get started, please install the library with:

pip install aspose-cells

You may also download it from the releases.

Gantt Charts with Stacked Bar Charts

Excel doesn’t provide a built-in Gantt chart type, and neither does Aspose.Cells. But you can still create one by using a stacked bar chart with a bit of formatting. This is a common technique, and Aspose.Cells fully supports it through code.

What You Can Do with Aspose.Cells

You can easily automate a Gantt chart in Excel by doing the following:

  • Enter task data (task name, start date, and duration).
  • Insert a stacked bar chart.
  • Use one series for the “start offset” (invisible) and another for the “duration” (visible).
  • Format the chart to look like a Gantt chart.

This gives you a professional-looking timeline that you can generate dynamically.

Sample Task Data

Here’s a sample table that we will use:

TaskStart DateDurationStart Offset
Task 12025-04-0130
Task 22025-04-0362
Task 32025-04-08107
Task 42025-04-18317
Task 52025-04-21520

Create a Gantt Chart in Excel using Python

Follow these steps to create a Gantt chart in Excel using Python with Aspose.Cells for Python via .NET:

  • Load an existing Excel data file using the Workbook class.
  • Get the first worksheet with the workbook.worksheets[0].
  • Add a stacked bar chart to add a Gantt chart layout.
  • Add two data series: one for the offset and one for the duration.
  • Set task names as category labels.
  • Adjust styles and axis settings to format the chart.
  • Save the file using the workbook.save() method.

Here’s a Python code snippet that performs all these steps:

This will generate a clean Gantt-style chart with your task data.

Create a Gantt Chart in Excel using Python

Create a Gantt Chart in Excel using Python

Customize the Gantt Chart

You can customize your Gantt chart using the following available options:

Bar Colors

Set a solid color for duration bars:

chart.n_series[1].area.foreground_color = Color.steel_blue

Transparent Offsets

Make the offset (invisible) bars fully transparent.

chart.n_series[0].area.formatting = cells.charts.FormattingType.NONE

Task Order

Reverse the Y-axis to show tasks from top to bottom:

chart.category_axis.is_plot_order_reversed = True

Data Labels

Show duration directly on each taskbar:

chart.n_series[1].data_labels.show_value = True

Legend Control

Remove or reposition the chart legend.

chart.show_legend = False

# Or reposition: 
chart.legend.position = cells.charts.LegendPositionType.BOTTOM

Set Custom Bar Colors

chart.n_series[1].points[0].area.foreground_color = Color.green
chart.n_series[1].points[1].area.foreground_color = Color.red

Resize the Chart

chart.chart_object.width = 700
chart.chart_object.height = 400

Export to PDF or Image

workbook.save("gantt_chart.pdf", cells.SaveFormat.PDF)
chart.to_image("chart.png")

The following Python code snippet applies some of the customizations mentioned above:

Customize Gantt Chart in Excel using Python

Customize Gantt Chart in Excel using Python

Get a Free License

You can request a free temporary license to explore the full capabilities of Aspose.Cells without any limitations or watermarks.

Additional Resources

Want to explore more? The links below will help you dive deeper into Aspose.Cells for Python via .NET. Whether you’re looking for detailed guides, API references, or free tools, these resources have you covered.

Final Words

Creating a Gantt chart in Excel with Python is simple and effective using Aspose.Cells for Python via .NET. It gives you complete control over chart design and Excel automation. Try it with your project data and see the difference.

If you run into any questions, please feel free to reach out at our free support forum.

See Also