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.

import aspose.slides as slides
import aspose.pydrawing as drawing
# Create presentation (or load existing one)
with slides.Presentation() as pres:
# Access first slide
sld = pres.slides[0]
# Add chart with default data
chart = sld.shapes.add_chart(slides.charts.ChartType.CLUSTERED_COLUMN, 0, 0, 500, 500)
# Set chart title
chart.chart_title.add_text_frame_for_overriding("Sample Title")
chart.chart_title.text_frame_for_overriding.text_frame_format.center_text = 1
chart.chart_title.height = 20
chart.has_title = True
# Set first series to show values
chart.chart_data.series[0].labels.default_data_label_format.show_value = True
# Set the index of chart data sheet
defaultWorksheetIndex = 0
# Get the chart data worksheet
fact = chart.chart_data.chart_data_workbook
# Delete default generated series and categories
chart.chart_data.series.clear()
chart.chart_data.categories.clear()
s = len(chart.chart_data.series)
s = len(chart.chart_data.categories)
# Add new series
chart.chart_data.series.add(fact.get_cell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.type)
chart.chart_data.series.add(fact.get_cell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.type)
# Add new categories
chart.chart_data.categories.add(fact.get_cell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"))
chart.chart_data.categories.add(fact.get_cell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"))
chart.chart_data.categories.add(fact.get_cell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"))
# Take first chart series
series = chart.chart_data.series[0]
# Populate series data
series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 1, 1, 20))
series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 2, 1, 50))
series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 3, 1, 30))
# Set fill color for series
series.format.fill.fill_type = slides.FillType.SOLID
series.format.fill.solid_fill_color.color = drawing.Color.blue
# Take second chart series
series = chart.chart_data.series[1]
# Populate series data
series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 1, 2, 30))
series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 2, 2, 10))
series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 3, 2, 60))
# Set fill color for series
series.format.fill.fill_type = slides.FillType.SOLID
series.format.fill.solid_fill_color.color = drawing.Color.orange
# First label will show category name
lbl = series.data_points[0].label
lbl.data_label_format.show_category_name = True
lbl = series.data_points[1].label
lbl.data_label_format.show_series_name = True
# Show value for third label
lbl = series.data_points[2].label
lbl.data_label_format.show_value = True
lbl.data_label_format.show_series_name = True
lbl.data_label_format.separator = "/"
# Save presentation
pres.save("column-chart.pptx", slides.export.SaveFormat.PPTX)

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.

import aspose.slides as slides
import aspose.pydrawing as drawing
# Create presentation (or load existing one)
with slides.Presentation() as pres:
# Access first slide
slide = pres.slides[0]
# Create the default chart
chart = slide.shapes.add_chart(slides.charts.ChartType.SCATTER_WITH_SMOOTH_LINES, 0, 0, 400, 400)
# Get the default chart data worksheet index
defaultWorksheetIndex = 0
# Get the chart data worksheet
fact = chart.chart_data.chart_data_workbook
# Delete demo series
chart.chart_data.series.clear()
# Add new series
chart.chart_data.series.add(fact.get_cell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.type)
chart.chart_data.series.add(fact.get_cell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.type)
# Take first chart series
series = chart.chart_data.series[0]
# Add new point (1:3)
series.data_points.add_data_point_for_scatter_series(fact.get_cell(defaultWorksheetIndex, 2, 1, 1), fact.get_cell(defaultWorksheetIndex, 2, 2, 3))
# Add new point (2:10)
series.data_points.add_data_point_for_scatter_series(fact.get_cell(defaultWorksheetIndex, 3, 1, 2), fact.get_cell(defaultWorksheetIndex, 3, 2, 10))
# Edit the type of series
series.type = slides.charts.ChartType.SCATTER_WITH_STRAIGHT_LINES_AND_MARKERS
# Change the chart series marker
series.marker.size = 10
series.marker.symbol = slides.charts.MarkerStyleType.STAR
# Take second chart series
series = chart.chart_data.series[1]
# Add new point (5:2)
series.data_points.add_data_point_for_scatter_series(fact.get_cell(defaultWorksheetIndex, 2, 3, 5), fact.get_cell(defaultWorksheetIndex, 2, 4, 2))
# Add new point (3:1)
series.data_points.add_data_point_for_scatter_series(fact.get_cell(defaultWorksheetIndex, 3, 3, 3), fact.get_cell(defaultWorksheetIndex, 3, 4, 1))
# Add new point (2:2)
series.data_points.add_data_point_for_scatter_series(fact.get_cell(defaultWorksheetIndex, 4, 3, 2), fact.get_cell(defaultWorksheetIndex, 4, 4, 2))
# Add new point (5:1)
series.data_points.add_data_point_for_scatter_series(fact.get_cell(defaultWorksheetIndex, 5, 3, 5), fact.get_cell(defaultWorksheetIndex, 5, 4, 1))
# Change the chart series marker
series.marker.size = 10
series.marker.symbol = slides.charts.MarkerStyleType.CIRCLE
# Save presentation
pres.save("scatter-chart.pptx", slides.export.SaveFormat.PPTX)

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.

import aspose.slides as slides
import aspose.pydrawing as drawing
# Create presentation (or load existing one)
with slides.Presentation() as presentation:
# Access first slide
slide = presentation.slides[0]
# Add chart with default data
chart = slide.shapes.add_chart(slides.charts.ChartType.PIE, 100, 100, 400, 400)
# Set chart title
chart.chart_title.add_text_frame_for_overriding("Sample Title")
chart.chart_title.text_frame_for_overriding.text_frame_format.center_text = 1
chart.chart_title.height = 20
chart.has_title = True
# Set first series to show values
chart.chart_data.series[0].labels.default_data_label_format.show_value = True
# Set the index of chart data sheet
defaultWorksheetIndex = 0
# Get the chart data worksheet
fact = chart.chart_data.chart_data_workbook
# Delete default generated series and categories
chart.chart_data.series.clear()
chart.chart_data.categories.clear()
# Add new categories
chart.chart_data.categories.add(fact.get_cell(0, 1, 0, "First Qtr"))
chart.chart_data.categories.add(fact.get_cell(0, 2, 0, "2nd Qtr"))
chart.chart_data.categories.add(fact.get_cell(0, 3, 0, "3rd Qtr"))
# Add new series
series = chart.chart_data.series.add(fact.get_cell(0, 0, 1, "Series 1"), chart.type)
# Populate series data
series.data_points.add_data_point_for_pie_series(fact.get_cell(defaultWorksheetIndex, 1, 1, 20))
series.data_points.add_data_point_for_pie_series(fact.get_cell(defaultWorksheetIndex, 2, 1, 50))
series.data_points.add_data_point_for_pie_series(fact.get_cell(defaultWorksheetIndex, 3, 1, 30))
# Add new points and set sector color
chart.chart_data.series_groups[0].is_color_varied = True
point = series.data_points[0]
point.format.fill.fill_type = slides.FillType.SOLID
point.format.fill.solid_fill_color.color = drawing.Color.orange
# Set sector border
point.format.line.fill_format.fill_type = slides.FillType.SOLID
point.format.line.fill_format.solid_fill_color.color = drawing.Color.gray
point.format.line.width = 3.0
# point.format.line.style = slides.LineStyle.THIN_THICK
# point.format.line.dash_style = slides.LineDashStyle.DASH_DOT
point1 = series.data_points[1]
point1.format.fill.fill_type = slides.FillType.SOLID
point1.format.fill.solid_fill_color.color = drawing.Color.blue_violet
# Set sector border
point1.format.line.fill_format.fill_type = slides.FillType.SOLID
point1.format.line.fill_format.solid_fill_color.color = drawing.Color.blue
point1.format.line.width = 3.0
# point1.format.line.style = slides.LineStyle.SINGLE
# point1.format.line.dash_style = slides.LineDashStyle.LARGE_DASH_DOT
point2 = series.data_points[2]
point2.format.fill.fill_type = slides.FillType.SOLID
point2.format.fill.solid_fill_color.color = drawing.Color.yellow_green
# Set sector border
point2.format.line.fill_format.fill_type = slides.FillType.SOLID
point2.format.line.fill_format.solid_fill_color.color = drawing.Color.red
point2.format.line.width = 2.0
# point2.format.line.style = slides.LineStyle.THIN_THIN
# point2.format.line.dash_style = slides.LineDashStyle.LARGE_DASH_DOT_DOT
# Create custom labels for each of categories for new series
lbl1 = series.data_points[0].label
# lbl.show_category_name = True
lbl1.data_label_format.show_value = True
lbl2 = series.data_points[1].label
lbl2.data_label_format.show_value = True
lbl2.data_label_format.show_legend_key = True
lbl2.data_label_format.show_percentage = True
lbl3 = series.data_points[2].label
lbl3.data_label_format.show_series_name = True
lbl3.data_label_format.show_percentage = True
# Show leader lines for chart
# series.labels.default_data_label_format.show_leader_lines = True
# Set rotation angle for pie chart sectors
chart.chart_data.series_groups[0].first_slice_angle = 180
# Save presentation
presentation.save("pie-chart.pptx", slides.export.SaveFormat.PPTX)

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.

import aspose.slides as slides
import aspose.pydrawing as drawing
# Create presentation (or load existing one)
with slides.Presentation() as pres:
# Add chart
chart = pres.slides[0].shapes.add_chart(slides.charts.ChartType.HISTOGRAM, 50, 50, 500, 400)
# Clear default categories and series
chart.chart_data.categories.clear()
chart.chart_data.series.clear()
# Access workbook
wb = chart.chart_data.chart_data_workbook
wb.clear(0)
# Add data points to series
series = chart.chart_data.series.add(slides.charts.ChartType.HISTOGRAM)
series.data_points.add_data_point_for_histogram_series(wb.get_cell(0, "A1", 15))
series.data_points.add_data_point_for_histogram_series(wb.get_cell(0, "A2", -41))
series.data_points.add_data_point_for_histogram_series(wb.get_cell(0, "A3", 16))
series.data_points.add_data_point_for_histogram_series(wb.get_cell(0, "A4", 10))
series.data_points.add_data_point_for_histogram_series(wb.get_cell(0, "A5", -23))
series.data_points.add_data_point_for_histogram_series(wb.get_cell(0, "A6", 16))
# Set aggregation type
chart.axes.horizontal_axis.aggregation_type = slides.charts.AxisAggregationType.AUTOMATIC
# Save presentation
pres.save("histogram-chart.pptx", slides.export.SaveFormat.PPTX)

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.

import aspose.slides as slides
import aspose.pydrawing as drawing
# Create presentation (or load existing one)
with slides.Presentation() as pres:
# Add chart
chart = pres.slides[0].shapes.add_chart(slides.charts.ChartType.OPEN_HIGH_LOW_CLOSE, 50, 50, 600, 400, False)
# Clear default series and categories
chart.chart_data.series.clear()
chart.chart_data.categories.clear()
# Access workbook
wb = chart.chart_data.chart_data_workbook
# Add categories
chart.chart_data.categories.add(wb.get_cell(0, 1, 0, "A"))
chart.chart_data.categories.add(wb.get_cell(0, 2, 0, "B"))
chart.chart_data.categories.add(wb.get_cell(0, 3, 0, "C"))
# Add series
chart.chart_data.series.add(wb.get_cell(0, 0, 1, "Open"), chart.type)
chart.chart_data.series.add(wb.get_cell(0, 0, 2, "High"), chart.type)
chart.chart_data.series.add(wb.get_cell(0, 0, 3, "Low"), chart.type)
chart.chart_data.series.add(wb.get_cell(0, 0, 4, "Close"), chart.type)
# Add data points
series = chart.chart_data.series[0]
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 1, 1, 72))
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 2, 1, 25))
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 3, 1, 38))
series = chart.chart_data.series[1]
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 1, 2, 172))
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 2, 2, 57))
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 3, 2, 57))
series = chart.chart_data.series[2]
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 1, 3, 12))
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 2, 3, 12))
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 3, 3, 13))
series = chart.chart_data.series[3]
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 1, 4, 25))
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 2, 4, 38))
series.data_points.add_data_point_for_stock_series(wb.get_cell(0, 3, 4, 50))
chart.chart_data.series_groups[0].up_down_bars.has_up_down_bars = True
chart.chart_data.series_groups[0].hi_low_lines_format.line.fill_format.fill_type = slides.FillType.SOLID
# Set series fill formatting
for ser in chart.chart_data.series:
ser.format.line.fill_format.fill_type = slides.FillType.NO_FILL
# Save presentation
pres.save("stock-chart.pptx", slides.export.SaveFormat.PPTX)

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