PPT PPTX
Aspose.Slides  for Python via .NET
PPTX

Create Charts in PPTX Presentations Using Python

Build Python applications that create customizable presentation charts with your own categories, data series, labels, and formatting.

Create a Pie Chart in a PPTX Presentation with Python

Aspose.Slides for Python via .NET lets you create a pie chart, replace its default categories and series, and customize its labels and colors. Pie charts work best when showing how a small number of numeric categories contribute to a whole. For more options, see Customize Pie Charts in Presentations .

Create a Pie Chart in a PPTX Presentation with Python

with slides.Presentation() as presentation:
    slide = presentation.slides[0]
    chart = slide.shapes.add_chart(slides.charts.ChartType.PIE, 100, 100, 400, 400)

    chart.has_title = True
    chart.chart_title.add_text_frame_for_overriding("Quarterly Sales")
    chart.chart_title.height = 20

    worksheet_index = 0
    workbook = chart.chart_data.chart_data_workbook
    chart.chart_data.series.clear()
    chart.chart_data.categories.clear()

    categories = ("First Quarter", "Second Quarter", "Third Quarter")
    values = (20, 50, 30)

    for row_index, category in enumerate(categories, start=1):
        category_cell = workbook.get_cell(worksheet_index, row_index, 0, category)
        chart.chart_data.categories.add(category_cell)

    series_name_cell = workbook.get_cell(worksheet_index, 0, 1, "Sales")
    series = chart.chart_data.series.add(series_name_cell, chart.type)

    for row_index, value in enumerate(values, start=1):
        value_cell = workbook.get_cell(worksheet_index, row_index, 1, value)
        series.data_points.add_data_point_for_pie_series(value_cell)

    series.labels.default_data_label_format.show_percentage = True
    series.parent_series_group.is_color_varied = True
    series.parent_series_group.first_slice_angle = 180

    fill_colors = (drawing.Color.orange, drawing.Color.blue_violet, drawing.Color.yellow_green)

    for data_point_index, fill_color in enumerate(fill_colors):
        data_point = series.data_points[data_point_index]
        data_point.format.fill.fill_type = slides.FillType.SOLID
        data_point.format.fill.solid_fill_color.color = fill_color

    presentation.save("pie-chart.pptx", slides.export.SaveFormat.PPTX)

How to Create and Customize a Pie Chart in PPTX with Python

Follow these steps to add a pie chart to a PPTX presentation.

  1. Create a Presentation instance and access the desired slide through a slide variable.

  2. Call add_chart with ChartType.PIE to add a pie chart to the slide.

  3. Use chart_data_workbook to replace the default categories and series, then configure the labels, colors, and first_slice_angle.

  4. Call save with SaveFormat.PPTX to write the presentation in PPTX format.

Create Charts in PPTX Online

Try the online chart creation demos.

Other Supported Chart Formats

You can also create charts in other supported presentation formats.