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.
- Create a
Presentationinstance and access the desired slide through aslidevariable. - Call
add_chartwithChartType.PIEto add a pie chart to the slide. - Use
chart_data_workbookto replace the default categories and series, then configure the labels, colors, andfirst_slice_angle. - Call
savewithSaveFormat.PPTXto write the presentation in PPTX format.
Built with Aspose.Slides
Create PPTX Charts Online
The free Aspose.Slides Chart app is built with Aspose.Slides for Python via .NET, the same API as the code above. Open it and see what the API can do with your own PPTX files.
- Runs in your browser, nothing to install.
- See how Aspose.Slides builds charts before you write code.
- Build a chart from Excel or table data.
- Download it as a PPTX file or an image.
Other Supported Chart Formats
Using Python, you can also create charts in other formats.