将Excel图表转换成图像
Contents
[
Hide
]
图表在视觉上很吸引人,可以帮助用户轻松地观察数据的比较、模式和趋势。例如,与分析工作表数字列相比,图表能够一眼看出销售额是下降还是上升,以及实际销售额与预期销售额的比较如何。人们经常需要以易于理解和维护的方式呈现统计和图形信息。图片有助于解释。
有时,在应用程序或网页中需要图表。或者可能需要将图表用于 Word 文档、PDF 文件、PowerPoint 演示文稿或其他应用程序中。在每种情况下,您希望将图表渲染为图像,以便在其他地方使用。
将图表转换为图像
在这些示例中,饼图和柱状图被转换为图像。
将饼图转换为图像文件
首先,在 Microsoft Excel 中创建一个饼图,然后使用 Aspose.Cells for Python via .NET 将其转换为图像文件。此示例中的代码基于模板 Microsoft Excel 文件中的饼图创建 EMF 图像。
输出:饼图图像 |
---|
![]() |
- 在 Microsoft Excel 中创建一个饼图:
- 在 Microsoft Excel 中打开一个新工作簿。
- 在工作表输入一些数据。
- 根据数据创建一个饼图。
- 保存文件。
输入文件。 |
---|
![]() |
我们将 Python 包托管在 PyPi 存储库中。
从 pypi 安装 Aspose.Cells for Python,请使用如下命令:$ pip install aspose-cells-python。
您还可以按照逐步说明来安装 “Aspose.Cells for Python via .NET” 到您的开发环境。
- 下载并安装 Aspose.Cells for Python via .NET:
从 pypi 安装 Aspose.Cells for Python via .NET,请使用如下命令:$ pip install aspose-cells-python。
- 您也可以遵循逐步说明,了解如何将"Aspose.Cells for Python via .NET"安装到您的开发环境中。
在首次安装时,所有Aspose组件均以评估模式运行。 评估模式没有时间限制,只会向输出文档中注入水印。
- 创建一个项目:
- 启动Visual Studio。
- 向您的Python项目添加库引用(导入库)。
- 编写查找并转换图表的代码。 以下是组件用于完成任务的代码。 使用的代码行很少。
This file contains hidden or 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
from aspose.cells import Workbook | |
from aspose.cells.drawing import ImageType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Open the existing excel file which contains the pie chart. | |
workbook = Workbook(dataDir + "PieChart.xlsx") | |
# Get the designer chart (first chart) in the first worksheet of the workbook. | |
chart = workbook.worksheets[0].charts[0] | |
# Convert the chart to an image file. | |
chart.to_image(dataDir + "PieChart.out.emf", ImageType.EMF) |
将柱状图转换为图像文件
首先在Microsoft Excel中创建柱状图,然后将其转换为图像文件,如上所示。执行示例代码后,基于模板Excel文件中的柱状图创建了一个JPEG文件。
输出文件:柱状图图像。 |
---|
![]() |
- 在Microsoft Excel中创建柱状图:
- 在Microsoft Excel中打开一个新的工作簿。
- 在工作表输入一些数据。
- 根据数据创建柱状图。
- 保存文件。
输入文件。 |
---|
![]() |
- 配置项目及引用,如上所述。
- 动态将图表转换为图像。 以下是组件用于完成任务的代码。 该代码与先前的代码类似。
This file contains hidden or 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
from aspose.cells import Workbook | |
from aspose.cells.drawing import ImageType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Open the existing excel file which contains the column chart. | |
workbook = Workbook(dataDir + "ColumnChart.xlsx") | |
# Get the designer chart (first chart) in the first worksheet of the workbook. | |
chart = workbook.worksheets[0].charts[0] | |
# Convert the chart to an image file. | |
chart.to_image(dataDir + "ColumnChart.out.jpeg", ImageType.JPEG) |