Read the complete article on how to create charts in PowerPoint PPT/PPTX in Python: https://blog.aspose.com/2022/02/16/create-charts-in-powerpoint-in-python/
Last active
February 16, 2022 14:52
-
-
Save aspose-com-gists/de5345d7b26f8e48dc5eac6787835b70 to your computer and use it in GitHub Desktop.
Create Charts in PowerPoint PPT or PPTX in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment