
图表是清晰简洁传达信息的强大工具。生成报告通常涉及以视觉方式呈现数据。在 Word 文档中加入图表可以显著提升数据的展示和理解。虽然 Microsoft Word 提供了内置的图表功能,但 Aspose.Words for Python 提供了以编程方式在 Word 文档中生成和集成图表的控制能力。在这篇博文中,我们将学习如何使用 Python 在 Word 文档中创建图表。
本文涵盖以下主题:
Python API 在 Word 文档中创建图表
我们将使用 Aspose.Words for Python 在 Word 文档中创建各种类型的图表。它是一个强大的库,允许您以编程方式创建、操作和转换 Word 文档。它提供了一个强大的 API,用于在 Word 文档中创建和自定义图表,是开发人员将数据可视化集成到文档生成工作流中的绝佳工具。这对于需要生成包含动态数据可视化的报告或文档的开发人员特别有用。
在开始创建图表之前,我们需要设置开发环境。按照以下步骤安装必要的软件包:
1. 安装 Aspose.Words for Python
请从下载页面下载软件包或通过在控制台中运行以下 pip 命令从 PyPI 安装它:
pip install aspose-words
2. 导入所需的库
import aspose.words as aw
from aspose.words import Document, DocumentBuilder, SaveFormat
from aspose.words.drawing import Shape
from aspose.words.drawing.charts import ChartType
如何在 Word 中创建柱状图
柱状图非常适合比较各类别的数据点。我们可以通过以下步骤在 Word 文档中轻松创建柱状图:
- 创建 Document 类的实例。
- 使用 DocumentBuilder 类创建文档。
- 使用 insert_chart() 方法添加图表,参数为 COLUMN ChartType、height 和 width。
- 在 Shape 类对象中获取结果。
- 创建 Chart 类的实例。
- 使用 chart.series 获取图表系列集合。
- 之后,定义数据并使用 add() 方法添加图表系列。
- 最后,调用 save() 方法保存文件。
以下代码示例展示了如何使用 Python 在 Word 文档中创建柱状图。
# This code example demonstrates how to create a columns chart in a Word document using Python. | |
# Create a document | |
doc = Document() | |
builder = DocumentBuilder(doc) | |
# Add chart with default data. You can specify different chart types and sizes. | |
shape = builder.insert_chart(ChartType.COLUMN, 432, 252) | |
# Chart property of Shape contains all chart related options. | |
chart = shape.chart | |
# Get chart series collection. | |
series_coll = chart.series | |
# Check series count. | |
print(series_coll.count) | |
# Delete default generated series. | |
series_coll.clear() | |
# Create category names array, in this example we have two categories. | |
categories = ["AW Category 1", "AW Category 2"] | |
# Adding new series. Please note, data arrays must not be empty and arrays must be the same size. | |
series_coll.add("AW Series 1", categories, [1, 2]) | |
series_coll.add("AW Series 2", categories, [3, 4]) | |
series_coll.add("AW Series 3", categories, [5, 6]) | |
series_coll.add("AW Series 4", categories, [7, 8]) | |
series_coll.add("AW Series 5", categories, [9, 10]) | |
# Save the document | |
doc.save("ColumnsChart.docx") |

使用 Python 在 Word 文档中创建柱状图
使用 Python 在 Word 文档中创建散点图
散点图对于显示两个变量之间的关系非常有用。我们可以按照之前提到的步骤在 Word 文档中插入散点图。不过,我们只需要在 insert_chart() 方法中设置 ChartType.SCATTER。
以下代码示例展示了如何使用 Python 在 Word 文档中创建散点图。
# This code example demonstrates how to create a scatter chart in a Word document using Python. | |
# Create a new document | |
doc = Document(); | |
builder = DocumentBuilder(doc); | |
# Insert Scatter chart. | |
shape = builder.insert_chart(ChartType.SCATTER, 432, 252); | |
chart = shape.chart; | |
# Use this overload to add series to any type of Scatter charts. | |
chart.series.add_double("Aspose Series 1", [ 0.7, 1.8, 2.6 ], [ 2.7, 3.2, 0.8 ]) | |
# Save the document | |
doc.save("ScatterChart.docx"); |

使用 Python 在 Word 文档中创建散点图
使用 Python 在 Word 文档中插入面积图
面积图强调随时间变化的幅度。我们可以通过以下步骤轻松在 Word 文档中创建面积图,不过我们只需要在 insert_chart() 方法中设置 ChartType.AREA。
以下代码示例展示了如何使用 Python 在 Word 文档中创建面积图。
# This code example demonstrates how to create a area chart in a Word document using Python. | |
# Create a new document | |
doc = Document(); | |
builder = DocumentBuilder(doc); | |
# Insert Area chart. | |
shape = builder.insert_chart(ChartType.AREA, 432, 252); | |
chart = shape.chart; | |
# Use this overload to add series to any type of Area, Radar and Stock charts. | |
chart.series.add_date("Aspose Series 1", | |
[ date(2002, 5, 1), date(2002, 6, 1), date(2002, 7, 1), date(2002, 8, 1), date(2002, 9, 1) ], | |
[ 32, 32, 28, 12, 15 ]) | |
# Save the document | |
doc.save("AreaChart.docx"); |

使用 Python 在 Word 文档中插入面积图
使用 Python 在 Word 文档中插入气泡图
气泡图有效地展示了数据的三个维度。通过以下步骤,我们可以轻松在 Word 文档中创建气泡图,不过我们需要在 insert_chart() 方法中设置 ChartType.BUBBLE。
以下代码示例展示了如何使用 Python 在 Word 文档中创建气泡图。
# This code example demonstrates how to create a bubble chart in a Word document using Python. | |
# Create a new document | |
doc = Document() | |
builder = DocumentBuilder(doc) | |
# Insert Bubble chart. | |
shape = builder.insert_chart(ChartType.BUBBLE, 432, 252) | |
# Add series | |
chart = shape.chart | |
chart.series.add("Aspose Series 1", [ 0.7, 1.8, 2.6 ], [ 2.7, 3.2, 0.8 ], [ 10, 4, 8 ]) | |
# Save the document | |
doc.save("BubbleChart.docx") |

使用 Python 在 Word 文档中插入气泡图
获取免费许可证
释放 Aspose.Words for Python 的全部功能!立即获取您的免费临时许可证,立即开始使用。没有限制,只是纯粹的文档操作魔法在您指尖。
Word 文档中的图表 – 实用资源
- 阅读官方文档中的 Working with Charts 部分,了解更多关于创建图表的信息。
除了在 Word 文档中使用图表外,请随意探索更多 Aspose.Words for Python 的功能,以进一步使用以下资源自定义图表和 Word 文档:
结论
在本文中,我们介绍了如何使用 Python 在 Word 文档中创建各种类型的图表——柱状图、散点图、面积图和气泡图。我们展示了如何利用 Aspose.Words for Python 在 Word 文档中创建各种类型的图表。通过遵循本文中的步骤,您可以轻松地将视觉效果出色的图表集成到您的 Word 文档中,使数据分析和
展示更加有效。
如果有任何疑问,请随时在我们的免费支持论坛联系我们寻求帮助。