Shape
Shape — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss 支持通过以下方式向工作表添加几何形状和文本框 ShapeCollection (ws.shapes). 每个形状由一个 Shape 对象,具有可配置的填充、线条和文本属性。.
Properties: aspose.cells_foss
from aspose.cells_foss import (
MsoDrawingType, FillType, MsoLineDashStyle,
TextAlignmentType, TextAnchorType,
MsoFillFormat, MsoLineFormat, ShapeFont, Shape,
)MsoDrawingType
MsoDrawingType 是一个 IntEnum 用于标识几何形状类型的.
| Properties | Properties | Properties | Properties |
|---|---|---|---|
UNKNOWN | 0 | TEXT_BOX | 21 |
RECTANGLE | 1 | CALLOUT | 22 |
ROUNDED_RECTANGLE | 2 | PENTAGON | 23 |
OVAL | 3 | CLOUD | 24 |
DIAMOND | 4 | HEART | 25 |
TRIANGLE | 5 | LIGHTNING_BOLT | 26 |
RIGHT_TRIANGLE | 6 | SMILEY_FACE | 27 |
PARALLELOGRAM | 7 | LEFT_RIGHT_ARROW | 28 |
TRAPEZOID | 8 | UP_DOWN_ARROW | 29 |
HEXAGON | 9 | CUBE | 30 |
OCTAGON | 10 | BEVEL | 31 |
CROSS | 11 | STAR_4 | 12 |
RIGHT_ARROW | 17 | STAR_5 | 13 |
LEFT_ARROW | 18 | STAR_6 | 14 |
UP_ARROW | 19 | STAR_7 | 15 |
DOWN_ARROW | 20 | STAR_8 | 16 |
FillType
FillType 是一个 IntEnum 用于标识形状所应用的填充类型的.
| Properties | Properties |
|---|---|
NONE | 0 |
SOLID | 1 |
GRADIENT | 2 |
PATTERN | 3 |
AUTOMATIC | 4 |
MsoLineDashStyle
MsoLineDashStyle 是一个 IntEnum 用于边框/轮廓线虚线模式的.
| Properties | Properties |
|---|---|
SOLID | 0 |
DASH | 1 |
DOT | 2 |
DASH_DOT | 3 |
DASH_DOT_DOT | 4 |
LONG_DASH | 5 |
LONG_DASH_DOT | 6 |
TextAlignmentType
TextAlignmentType 是一个 IntEnum 用于形状内部水平文本对齐的.
| Properties | Properties |
|---|---|
LEFT | 0 |
CENTER | 1 |
RIGHT | 2 |
JUSTIFY | 3 |
DISTRIBUTED | 4 |
TextAnchorType
TextAnchorType 是一个 IntEnum 用于形状内部垂直文本锚定的.
| Properties | Properties |
|---|---|
TOP | 0 |
MIDDLE | 1 |
BOTTOM | 2 |
JUSTIFIED | 3 |
MsoFillFormat
控制形状的填充外观。通过以下方式访问 shape.fill.
Properties
| Properties | Properties | Properties |
|---|---|---|
fill_type | FillType | 当前活动的填充类型。. |
fore_color | str | 前景(主要)填充颜色,使用 RRGGBB 十六进制表示。. |
transparency | float | 填充不透明度为一个值,从 0.0 (完全不透明)到 1.0 (完全透明)。. |
Properties
copy
fill.copy() -> MsoFillFormat返回这些填充设置的深拷贝。.
MsoLineFormat
控制形状的轮廓/边框外观。通过 shape.line.
Properties
| Properties | Properties | Properties |
|---|---|---|
is_visible | bool | True 用于绘制形状轮廓。. |
color | str | 轮廓颜色使用 RRGGBB 十六进制表示。. |
weight | int | 轮廓粗细(厚度),单位为点。. |
dash_style | MsoLineDashStyle | 轮廓的虚线模式。. |
transparency | float | 轮廓不透明度从 0.0 (不透明)到 1.0 (透明)。. |
Properties
copy
line.copy() -> MsoLineFormat返回这些线条设置的深拷贝。.
ShapeFont
控制形状内文本的字体。通过以下方式访问 shape.font.
Properties
| Properties | Properties | Properties |
|---|---|---|
name | str | 字体族名称(例如,., "Calibri"). |
size | float | 字体大小,单位为点。. |
bold | bool | True 用于加粗。. |
italic | bool | True 用于斜体。. |
underline | bool | True 用于单下划线。. |
color | str | 字体颜色使用 RRGGBB 十六进制表示。. |
Properties
copy
font.copy() -> ShapeFont返回这些字体设置的深拷贝。.
Properties
Shape 对象不能直接构造。请使用 ws.shapes.add(...) 或 ws.shapes.add_text_box(...).
Properties
| Properties | Properties | Properties | Properties |
|---|---|---|---|
name | str | 读/写 | 形状名称(必须在工作表中唯一)。. |
drawing_type | MsoDrawingType | Properties | 此形状的几何类型。. |
upper_left_row | int | 读/写 | 0-based row index of the top-left anchor cell. |
upper_left_column | int | 读/写 | 0-based column index of the top-left anchor cell. |
lower_right_row | int | 读/写 | 0-based row index of the bottom-right anchor cell. |
lower_right_column | int | 读/写 | 0-based column index of the bottom-right anchor cell. |
upper_left_row_offset | int | 读/写 | 像素偏移量,从左上单元格的顶部边缘算起。. |
upper_left_column_offset | int | 读/写 | 像素偏移量,从左上单元格的左侧边缘算起。. |
lower_right_row_offset | int | 读/写 | 像素偏移量,从右下单元格的顶部边缘算起。. |
lower_right_column_offset | int | 读/写 | 像素偏移量,从右下单元格的左侧边缘算起。. |
fill | MsoFillFormat | Properties | 填充(背景)设置。. |
line | MsoLineFormat | Properties | 轮廓/边框设置。. |
text | str | 读/写 | 形状内部显示的文本内容。. |
font | ShapeFont | Properties | 形状文本的字体设置。. |
text_horizontal_alignment | TextAlignmentType | 读/写 | 形状内部的水平文本对齐。. |
text_vertical_alignment | TextAnchorType | 读/写 | 形状内部的垂直文本锚定。. |
is_text_wrapped | bool | 读/写 | True 用于在形状边界换行文本。. |
is_locked | bool | 读/写 | True 用于在工作表保护激活时锁定形状。. |
is_hidden | bool | 读/写 | True 用于隐藏形状。. |
hyperlink | `str | None` | 读/写 |
Properties
copy
shape.copy() -> Shape返回此形状的深拷贝,保留所有格式设置。.
ShapeCollection
ShapeCollection 可访问为 ws.shapes. 它包含工作表上的所有形状。.
Properties
add
ws.shapes.add(
drawing_type: MsoDrawingType,
upper_left_row: int,
upper_left_column: int,
lower_right_row: int,
lower_right_column: int
) -> Shape添加指定类型的形状。所有位置参数均为从 0 开始的行/列索引。.
add_text_box
ws.shapes.add_text_box(
upper_left_row: int,
upper_left_column: int,
lower_right_row: int,
lower_right_column: int
) -> Shape快捷方式 add(MsoDrawingType.TEXT_BOX, ...).
属性和迭代
| Properties | Properties |
|---|---|
count | 集合中形状的数量(只读)。. |
__len__() | Properties count. |
__iter__() | 遍历所有 Shape 对象。. |
__getitem__(index) | 返回 Shape (基于零索引) index. |
Properties
添加实心填充的矩形
from aspose.cells_foss import Workbook, MsoDrawingType, FillType, MsoLineDashStyle, TextAlignmentType
wb = Workbook()
ws = wb.worksheets[0]
# Add a blue rectangle spanning rows 1-5, columns 1-4 (0-based)
rect = ws.shapes.add(MsoDrawingType.RECTANGLE, 1, 1, 5, 4)
rect.name = "BlueBox"
rect.fill.fill_type = FillType.SOLID
rect.fill.fore_color = "4472C4" # blue
rect.fill.transparency = 0.0
rect.line.is_visible = True
rect.line.color = "1F3864" # dark blue
rect.line.weight = 2
rect.line.dash_style = MsoLineDashStyle.SOLID
rect.text = "Sales Summary"
rect.font.bold = True
rect.font.color = "FFFFFF" # white text
rect.font.size = 12
rect.text_horizontal_alignment = TextAlignmentType.CENTER
wb.save("shapes.xlsx")添加文本框
from aspose.cells_foss import Workbook
wb = Workbook()
ws = wb.worksheets[0]
tb = ws.shapes.add_text_box(2, 0, 5, 3)
tb.name = "NoteBox"
tb.text = "Review this section before publishing."
tb.font.italic = True
tb.font.color = "595959"
tb.is_text_wrapped = True
tb.line.is_visible = True
tb.line.color = "BFBFBF"
wb.save("textbox.xlsx")