
在 Word 中的圓餅圖是一個強大的視覺工具。它有助於以清晰和引人入勝的方式呈現數據。許多行業使用圓餅圖來顯示比例和比較。例如,企業用它來顯示銷售數據;教育工作者用它來展示調查結果。一個設計良好的圓餅圖可以增強報告和簡報,使複雜的信息更易於理解。在這篇博客中,我們將探索如何使用 Python 在 Word 中建立圓餅圖。
本文涵蓋以下主題:
Python 圓餅圖 Word 函式庫
我們將使用 Aspose.Words for Python 在 Word 文件中建立圓餅圖。這個函式庫簡化了建立和編輯 Word 文件的過程。使用 Aspose.Words,您可以輕鬆地將圖表(包括圓餅圖)插入文件中。該函式庫支持各種圖表類型並提供先進的自定義選項,這使其成為希望自動生成文件的開發者的理想選擇。
此 Python 圓餅圖函式庫提供了多個功能,使其成為在 Word 中建立圓餅圖的理想工具:
- 易於整合:該函式庫與 Python 應用程式完美整合。
- 靈活性:您可以程式化地建立、修改和操控文件。
- 先進的自定義選項:輕鬆自定義圓餅圖的外觀和數據。
- 高效能:快速生成文件而不損失品質。
要開始,您需要安裝 Aspose.Words for Python。您可以從發布區下載它,並使用以下 pip 命令進行安裝:
pip install aspose-words
如何在 Word 中建立圓餅圖
按照以下步驟,使用 Python 在 Word 文件中建立圓餅圖:
- 使用 Document 類建立一個新的 Word 文件。
- 使用 DocumentBuilder 物件來構建文件。
- 使用 insert_chart() 方法插入具有指定尺寸的圓餅圖。
- 使用 chartShape.chart 從插入的圖形中檢索圖表。
- 使用 chart.series.clear() 清除圖表中的現有系列。
- 添加帶有類別和數值的新系列。
- 自定義數據標籤。
- 自定義外觀。
- 使用 save() 方法將文件保存到指定路徑。
以下是實現上述步驟的完整 Python 程式碼範例:
import aspose.words as aw | |
import aspose.pydrawing as drawing | |
from aspose.words import Document, DocumentBuilder | |
from aspose.words.drawing import Shape | |
from aspose.words.drawing.charts import ChartType | |
# Step 1: Create a Document | |
doc = aw.Document() | |
# Step 2: Add a Pie Chart | |
builder = aw.DocumentBuilder(doc) | |
chartShape = builder.insert_chart(ChartType.PIE, 400, 300) | |
chart = chartShape.chart; | |
# Step 3: Set Data | |
chart.series.clear() | |
series = chart.series.add("Aspose Series 1", | |
["Category 1", "Category 2", "Category 3"], | |
[2.7, 3.2, 0.8]) | |
labels = series.data_labels | |
labels.show_percentage = True | |
labels.show_value = True | |
labels.show_leader_lines = False | |
labels.separator = " - " | |
# Step 4: Customize Appearance | |
# Give our chart a title, which appears at the top center of the chart area. | |
title = chart.title; | |
title.text = "Sample Pie Chart"; | |
title.font.size = 15; | |
title.font.color = drawing.Color.blue; | |
title.show = True; | |
title.overlay = True; | |
# Step 5: Save the Document | |
doc.save("PieChartInWord.docx") |

如何使用 Python 在 Word 中建立圓餅圖。
在 Word 中建立餅中餅圖
要建立 “餅中餅” 圓餅圖,只需將 ChartType.PIE_OF_PIE 指定為 ChartType.PIE 即可。
以下是帶有此修改的程式碼:
import aspose.words as aw | |
import aspose.pydrawing as drawing | |
from aspose.words import Document, DocumentBuilder | |
from aspose.words.drawing import Shape | |
from aspose.words.drawing.charts import ChartType | |
# Create a document. | |
doc = aw.Document(); | |
builder = aw.DocumentBuilder(doc); | |
# Insert a Pio of Pie chart | |
shape = builder.insert_chart(ChartType.PIE_OF_PIE, 440, 300); | |
chart = shape.chart; | |
# Delete the default generated series. | |
chart.series.clear(); | |
# Add series | |
series = chart.series.add("Aspose Series 1", | |
["Category 1", "Category 2", "Category 3", "Category 4"], | |
[11, 8, 4, 3]) | |
# Format the Pie of Pie chart. | |
seriesGroup = chart.series_groups[0]; | |
seriesGroup.gap_width = 10; | |
seriesGroup.second_section_size = 77; | |
doc.save("PieOfPieChart.docx"); |

如何使用 Python 在 Word 中建立餅中餅圖。
在 Word 中建立餅柱圖
要建立 “餅柱” 圖表,您只需將步驟 2 修改為使用 ChartType.PIE_OF_BAR 代替 ChartType.PIE。以下是更新的程式碼:
import aspose.words as aw | |
import aspose.pydrawing as drawing | |
from aspose.words import Document, DocumentBuilder | |
from aspose.words.drawing import Shape | |
from aspose.words.drawing.charts import ChartType | |
# Step 1: Create a Document | |
doc = aw.Document() | |
# Step 2: Add a Pie of Bar Chart | |
builder = aw.DocumentBuilder(doc) | |
chartShape = builder.insert_chart(ChartType.PIE_OF_BAR, 400, 300) # Change to PIE_OF_BAR | |
chart = chartShape.chart | |
# Step 3: Set Data | |
chart.series.clear() | |
series = chart.series.add("Aspose Series 1", | |
["Category 1", "Category 2", "Category 3", "Category 4", "Category 5"], | |
[2.7, 3.2, 0.8, 1.2, 0.5]) | |
labels = series.data_labels | |
labels.show_percentage = True | |
labels.show_value = True | |
labels.show_leader_lines = False | |
labels.separator = " - " | |
# Step 4: Customize Appearance | |
title = chart.title | |
title.text = "Sample Pie of Bar Chart" | |
title.font.size = 15 | |
title.font.color = drawing.Color.blue | |
title.show = True | |
title.overlay = True | |
# Step 5: Save the Document | |
doc.save("PieOfBarChartInWord.docx") |

如何使用 Python 在 Word 中建立餅柱圖。
在 Word 中建立 3D 圓餅圖
要建立 3D 圓餅圖,只需將步驟 2 更新為指定 ChartType.PIE_3D。以下是更新的程式碼:
import aspose.words as aw | |
import aspose.pydrawing as drawing | |
from aspose.words import Document, DocumentBuilder | |
from aspose.words.drawing import Shape | |
from aspose.words.drawing.charts import ChartType | |
# Step 1: Create a Document | |
doc = aw.Document() | |
# Step 2: Add a 3D Pie Chart | |
builder = aw.DocumentBuilder(doc) | |
chartShape = builder.insert_chart(ChartType.PIE_3D, 400, 300) # Change to PIE_3D | |
chart = chartShape.chart | |
# Step 3: Set Data | |
chart.series.clear() | |
series = chart.series.add("Aspose Series 1", | |
["Category 1", "Category 2", "Category 3"], | |
[2.7, 3.2, 0.8]) | |
labels = series.data_labels | |
labels.show_percentage = True | |
labels.show_value = True | |
labels.show_leader_lines = False | |
labels.separator = " - " | |
# Step 4: Customize Appearance | |
chart.title.text = "Sample 3D Pie Chart" | |
# Step 5: Save the Document | |
doc.save("3DPieChartInWord.docx") |

如何使用 Python 在 Word 中建立 3D 圓餅圖。
獲取免費許可
有興趣探索 Aspose 產品嗎?訪問許可頁面以獲取 Aspose.Words for Python 的免費臨時許可。輕鬆開始,體驗函式庫的完整功能,不要錯過這個機會!
圓餅圖在 Word 中的免費資源
除了在 Word 文件中建立圓餅圖,我們鼓勵您探索更多的資源。這些資源將幫助您進一步提升對 Aspose.Words for Python 的理解和技能。
結論
在這篇博客中,我們探索了如何使用 Python 在 Word 中建立圓餅圖。我們討論了函式庫的特點並提供了逐步指南和程式碼範例。使用 Aspose.Words,您可以輕鬆地自動生成文件並使用視覺數據增強您的報告。探索更多 Aspose.Words for Python 以釋放其全部潛力。
如果您有任何問題或需要進一步的幫助,請隨時在我們的免費支持論壇上聯絡我們。