管理图片
Contents
[
Hide
]
Aspose.Cells for Python via .NET 允许开发者在运行时向电子表格添加图片。此外,可以在运行时控制这些图片的定位,详细内容将在接下来的部分中讨论。
本文章介绍了如何添加图片,以及如何插入显示特定单元格内容的图片。
添加图片
向电子表格中添加图片非常简单。只需几行代码: 只需调用Worksheet中封装的pictures集合的add方法。add 方法接受以下参数:
- 左上角行索引,左上角行的索引。
- 左上角列索引,左上角列的索引。
- 图像文件名,图像文件的名称,包括完整路径。
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 os import os, path | |
# 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(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding a picture at the location of a cell whose row and column indices | |
# Are 5 in the worksheet. It is "F6" cell | |
worksheet.pictures.add(5, 5, dataDir + "logo.jpg") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
定位图片
使用 Aspose.Cells for Python via .NET 可以通过两种方法控制图片位置:
- 比例定位:定义相对于行高和列宽的位置。
- 绝对定位:定义图片将插入到页面上的确切位置,例如,距离单元格左侧40像素,距离边缘下方20像素。
比例定位
开发人员可以使用Aspose.Cells.Drawing.Picture对象的upper_delta_x和upper_delta_y属性将图片定位于行高和列宽的比例。可以通过传递其图片索引从pictures集合获取Picture对象。此示例将图像放置在F6单元格中。
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 os import os, path | |
# 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(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding a picture at the location of a cell whose row and column indices | |
# Are 5 in the worksheet. It is "F6" cell | |
pictureIndex = worksheet.pictures.add(5, 5, dataDir + "logo.jpg") | |
# Accessing the newly added picture | |
picture = worksheet.pictures[pictureIndex] | |
# Positioning the picture proportional to row height and colum width | |
picture.upper_delta_x = 200 | |
picture.upper_delta_y = 200 | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
绝对定位
开发人员还可以使用Picture对象的left和top属性绝对定位图片。此示例将图像放置在F6单元格中,离左侧60像素,离顶部10像素。
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 | |
# 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(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding a picture at the location of a cell whose row and column indices | |
# Are 5 in the worksheet. It is "F6" cell | |
pictureIndex = worksheet.pictures.add(5, 5, dataDir + "logo.jpg") | |
# Accessing the newly added picture | |
picture = worksheet.pictures[pictureIndex] | |
# Absolute positioning of the picture in unit of pixels | |
picture.left = 60 | |
picture.top = 10 | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
基于单元格引用插入图片
Aspose.Cells for Python via .NET 允许你在图形形状中显示工作表单元格的内容。你可以将图片链接到包含你想显示数据的单元格。由于单元格或单元格区域与图形对象关联,因此你对该单元格或区域的数据所做的更改会自动反映在图形对象中。
通过调用ShapeCollection对象中封装的add_picture集合的Picture属性指定单元格范围,将图片添加到工作表中。
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 | |
# 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(".") | |
# Instantiate a new Workbook | |
workbook = Workbook() | |
# Get the first worksheet's cells collection | |
cells = workbook.worksheets[0].cells | |
# Add string values to the cells | |
cells.get("A1").put_value("A1") | |
cells.get("C10").put_value("C10") | |
# Add a blank picture to the D1 cell | |
pic = workbook.worksheets[0].shapes.add_picture(0, 3, 10, 6, None) | |
# Specify the formula that refers to the source range of cells | |
pic.formula = "A1:C10" | |
# Update the shapes selected value in the worksheet | |
workbook.worksheets[0].shapes.update_selected_value() | |
# Save the Excel file. | |
workbook.save(dataDir + "output.out.xls") |