PPT
PPTX
PPT
使用 Python 將 PowerPoint 圖表和圖表添加到 PPT
構建您自己的 Python 應用程序,以使用服務器端 API 在演示文稿文件中創建完全可定制的圖表。了解如何向圖表添加新系列和類別。
通過 Python 將餅圖添加到 PPT 演示文稿
餅圖用於顯示數據中部分與整體的關係,尤其是當數據包含帶有數值的分類標籤時。 Aspose.Slides for Python via .NET 提供了一個強大的 API,用於在 PowerPoint 演示文稿中創建餅圖。您可以在此 鏈接 中找到有關使用 Aspose.Slides for Python API 自定義餅圖的更多信息。
以下代碼示例顯示瞭如何在 Python 中的 PowerPoint PPT 中創建餅圖。
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 = slides.NullableBool(True)
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
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.ppt", slides.export.SaveFormat.PPT)
如何通過 Python 在 PPT 中創建和自定義餅圖
這些是將餅圖添加到 PPT 文件的步驟。
首先,創建 PPT Presentation 類的一個實例。
使用 add_chart(…) 方法添加一個帶有默認數據的餅圖。
配置餅圖的必要屬性(設置圖表標題,修改圖表數據,將格式應用於系列和類別)
以 PPT 格式保存結果