Create Charts in PowerPoint Presentations in Python

Charts are used for the graphical representation of the data, which makes the analysis easier. Therefore, MS PowerPoint supports a range of charts to visualize the data in different ways. Among all, the most commonly used charts include pie charts, line charts, bar charts, histograms, stock charts, etc. In this article, you will learn how to create these charts in PowerPoint PPT or PPTX in Python.

Python Library to Create Charts in PowerPoint PPT

To create charts in PowerPoint PPT/PPTX, we will use Aspose.Slides for Python via .NET. It is a feature-rich library that provides a complete package to create and manipulate PowerPoint presentations. You can install it from PyPI using the following pip command.

> pip install aspose.slides

Aspose.Slides supports a variety of charts that can be added to the presentations dynamically. In the following sections, we will demonstrate how to create some popular types of charts.

Create Column Chart in PowerPoint PPT in Python

In this section, you will learn how to create a column chart in a PowerPoint presentation in Python. The following are the steps to perform this operation.

  • First, create an instance of the Presentation class.
  • Get the reference of the slide from Presentations.slides in an object.
  • Add a Clustered Column chart with default data using Slide.shapes.add_chart() method.
  • Set chart title and other properties such as text formatting.
  • Access the chart data workbook into an object using Chart.chart_data.chart_data_workbook() method.
  • Clear all the default series and categories from chart data using Chart.chart_data.series.clear() and Chart.chart_data.categories.clear() methods respectively.
  • Add new series and categories.
  • Access each chart series into an object and add data points to it.
  • Add fill color for chart series and set labels.
  • Finally, save the presentation using Presentation.save(string, SaveFormat) method.

The following code sample shows how to create a column chart in PowerPoint PPT in Python.

The following is the screenshot of the resultant column chart.

create column chart in powerpoint in Python

Create Scatter Chart in PowerPoint PPT in Python

The following are the steps to create a scatter chart in the PowerPoint PPT in Python.

  • First, create an instance of the Presentation class.
  • Get the reference of the slide from Presentations.slides in an object.
  • Add a Scatter chart with default data using Slide.shapes.add_chart() method.
  • Access the chart data workbook into an object using Chart.chart_data.chart_data_workbook() method.
  • Add new series to the chart data.
  • Access each series into an object and add data points to the series.
  • Set the marker for the series.
  • Finally, save the presentation using Presentation.save(string, SaveFormat) method.

The following code sample shows how to create a scatter chart in PowerPoint PPTX in Python.

The following screenshot shows the resultant scatter chart.

create scatter chart in powerpoint in Python

Create Pie Chart in PowerPoint PPT in Python

The following are the steps to create a pie chart in a PowerPoint PPT in Python.

  • First, create an instance of the Presentation class.
  • Get the reference of the slide from Presentations.slides in an object.
  • Add a Pie chart with default data using Slide.shapes.add_chart() method.
  • Set chart title and other properties such as text formatting.
  • Set visibility of the values.
  • Clear all the default series and categories from chart data using Chart.chart_data.series.clear() and Chart.chart_data.categories.clear() methods respectively.
  • Access the chart data workbook into an object using Chart.chart_data.chart_data_workbook() method.
  • Add new categories to the chart data.
  • Add new series to the chart data.
  • Access each series into an object and add data points to the series.
  • Access each data point and set its formatting.
  • Apply formatting to data labels in the data points.
  • Set leader lines and rotation angles.
  • Finally, save the presentation using Presentation.save(string, SaveFormat) method.

The following code sample shows how to create a pie chart in PowerPoint PPTX in Python.

The following is the screenshot of the generated pie chart.

create pie chart in powerpoint in Python

Add Histogram Chart in PowerPoint PPTX in Python

The following are the steps to create a histogram chart in PowerPoint PPT using Aspose.Slides for Python.

  • First, create an instance of the Presentation class.
  • Get the reference of the slide from Presentations.slides in an object.
  • Add a Histogram chart with default data using Slide.shapes.add_chart() method.
  • Clear the default series and categories.
  • Access the chart data workbook into an object using Chart.chart_data.chart_data_workbook() method.
  • Add new series to the chart data.
  • Access each series into an object and add data points to the series.
  • Set aggregation type of the chart axis.
  • Finally, save the presentation using Presentation.save(string, SaveFormat) method.

The following code sample shows how to create a histogram chart in PPTX in Python.

The following is the screenshot of the created histogram chart.

create histogram chart in powerpoint in Python

Create a Stock Chart in PowerPoint using Python

Stock charts are also among the commonly used chart types in PowerPoint presentations. The following are the steps to create a stock chart in a PPT in Python.

  • First, create an instance of the Presentation class.
  • Get the reference of the slide from Presentations.slides in an object.
  • Add a Open High Low Close chart with default data using Slide.shapes.add_chart() method.
  • Clear the default series and categories.
  • Access the chart data workbook into an object using Chart.chart_data.chart_data_workbook() method.
  • Add new series and categories to the chart.
  • Access each chart series and add data points.
  • Specify Hi Low Lines format.
  • Finally, save the presentation using Presentation.save(string, SaveFormat) method.

The following code sample shows how to add a stock chart to PowerPoint PPTX in Python.

The following is the screenshot of the created stock chart.

create stock chart in powerpoint in Python

More about Charts

There are a lot of other charts that you can add to the PowerPoint presentations using Aspose.Slides for Python. To read more about the supported chart types, you can visit this documentation article.

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, you have learned how to create charts in PowerPoint PPT or PPTX in Python. We have gone through the steps and code samples of how to add column charts, scatter charts, pie charts, histograms, and stock charts. You can explore more about Aspose.Slides for Python using documentation. In case you would have any questions or queries, let us know via our forum.

See Also