# Tạo Biểu đồ trong Tệp Bản trình bày PPTX bằng Python

> Mã nguồn Python để tạo biểu đồ trong Bản trình bày PPTX.

Source: https://products.aspose.com/slides/vi/python-net/chart/pptx/
Updated: 2023-06-01



## Thêm Biểu đồ hình tròn vào Bản trình bày PPTX qua Python

Biểu đồ hình tròn được sử dụng để hiển thị mối quan hệ từng phần trong dữ liệu, đặc biệt khi dữ liệu chứa các nhãn phân loại có giá trị số. Aspose.Slides for Python via .NET cung cấp API mạnh mẽ để tạo biểu đồ Hình tròn trong bản trình bày PowerPoint. Bạn có thể tìm thêm thông tin về cách tùy chỉnh biểu đồ hình tròn bằng API Aspose.Slides cho Python trong [liên kết](https://docs.aspose.com/slides/python-net/pie-chart/) này.

### Mẫu mã sau đây cho biết cách tạo biểu đồ hình tròn trong PowerPoint PPTX trong Python.

```py

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.pptx", slides.export.SaveFormat.PPTX)

```

## Cách tạo và tùy chỉnh Biểu đồ hình tròn trong PPTX qua Python

Đây là các bước để thêm biểu đồ Hình tròn vào tệp PPTX.

Đầu tiên, tạo một thể hiện của lớp Trình bày PPTX.

Thêm biểu đồ hình tròn với dữ liệu mặc định bằng phương thức add_chart(...)

Định cấu hình các thuộc tính cần thiết của biểu đồ Hình tròn (Đặt tiêu đề biểu đồ, sửa đổi dữ liệu biểu đồ, Áp dụng định dạng cho chuỗi và danh mục)

Lưu kết quả ở định dạng PPTX

## Các định dạng được hỗ trợ khác
