Shape — Aspose.Cells FOSS for Python API Reference

aspose-cells-foss supports adding geometric shapes and text boxes to worksheets through the ShapeCollection (ws.shapes). Each shape is represented by a Shape object with configurable fill, line, and text properties.

Package: aspose.cells_foss

from aspose.cells_foss import (
    MsoDrawingType, FillType, MsoLineDashStyle,
    TextAlignmentType, TextAnchorType,
    MsoFillFormat, MsoLineFormat, ShapeFont, Shape,
)

MsoDrawingType

MsoDrawingType is an IntEnum identifying the geometric shape type.

MemberValueMemberValue
UNKNOWN0TEXT_BOX21
RECTANGLE1CALLOUT22
ROUNDED_RECTANGLE2PENTAGON23
OVAL3CLOUD24
DIAMOND4HEART25
TRIANGLE5LIGHTNING_BOLT26
RIGHT_TRIANGLE6SMILEY_FACE27
PARALLELOGRAM7LEFT_RIGHT_ARROW28
TRAPEZOID8UP_DOWN_ARROW29
HEXAGON9CUBE30
OCTAGON10BEVEL31
CROSS11STAR_412
RIGHT_ARROW17STAR_513
LEFT_ARROW18STAR_614
UP_ARROW19STAR_715
DOWN_ARROW20STAR_816

FillType

FillType is an IntEnum identifying the type of fill applied to a shape.

MemberValue
NONE0
SOLID1
GRADIENT2
PATTERN3
AUTOMATIC4

MsoLineDashStyle

MsoLineDashStyle is an IntEnum for the border/outline line dash pattern.

MemberValue
SOLID0
DASH1
DOT2
DASH_DOT3
DASH_DOT_DOT4
LONG_DASH5
LONG_DASH_DOT6

TextAlignmentType

TextAlignmentType is an IntEnum for horizontal text alignment within a shape.

MemberValue
LEFT0
CENTER1
RIGHT2
JUSTIFY3
DISTRIBUTED4

TextAnchorType

TextAnchorType is an IntEnum for vertical text anchoring within a shape.

MemberValue
TOP0
MIDDLE1
BOTTOM2
JUSTIFIED3

MsoFillFormat

Controls the fill appearance of a shape. Accessed via shape.fill.

Properties

PropertyTypeDescription
fill_typeFillTypeThe active fill type.
fore_colorstrForeground (primary) fill color as RRGGBB hex.
transparencyfloatFill opacity as a value from 0.0 (fully opaque) to 1.0 (fully transparent).

Methods

copy

fill.copy() -> MsoFillFormat

Returns a deep copy of these fill settings.


MsoLineFormat

Controls the outline/border appearance of a shape. Accessed via shape.line.

Properties

PropertyTypeDescription
is_visibleboolTrue to draw the shape outline.
colorstrOutline color as RRGGBB hex.
weightintOutline weight (thickness) in points.
dash_styleMsoLineDashStyleDash pattern for the outline.
transparencyfloatOutline opacity from 0.0 (opaque) to 1.0 (transparent).

Methods

copy

line.copy() -> MsoLineFormat

Returns a deep copy of these line settings.


ShapeFont

Controls the text font within a shape. Accessed via shape.font.

Properties

PropertyTypeDescription
namestrFont family name (e.g., "Calibri").
sizefloatFont size in points.
boldboolTrue for bold weight.
italicboolTrue for italic style.
underlineboolTrue for single underline.
colorstrFont color as RRGGBB hex.

Methods

copy

font.copy() -> ShapeFont

Returns a deep copy of these font settings.


Shape

Shape objects are not constructed directly. Use ws.shapes.add(...) or ws.shapes.add_text_box(...).

Properties

PropertyTypeAccessDescription
namestrRead/WriteShape name (must be unique within the worksheet).
drawing_typeMsoDrawingTypeReadThe geometric type of this shape.
upper_left_rowintRead/Write0-based row index of the top-left anchor cell.
upper_left_columnintRead/Write0-based column index of the top-left anchor cell.
lower_right_rowintRead/Write0-based row index of the bottom-right anchor cell.
lower_right_columnintRead/Write0-based column index of the bottom-right anchor cell.
upper_left_row_offsetintRead/WritePixel offset from the top edge of the top-left cell.
upper_left_column_offsetintRead/WritePixel offset from the left edge of the top-left cell.
lower_right_row_offsetintRead/WritePixel offset from the top edge of the bottom-right cell.
lower_right_column_offsetintRead/WritePixel offset from the left edge of the bottom-right cell.
fillMsoFillFormatReadFill (background) settings.
lineMsoLineFormatReadOutline/border settings.
textstrRead/WriteText content displayed inside the shape.
fontShapeFontReadFont settings for the shape text.
text_horizontal_alignmentTextAlignmentTypeRead/WriteHorizontal text alignment inside the shape.
text_vertical_alignmentTextAnchorTypeRead/WriteVertical text anchoring inside the shape.
is_text_wrappedboolRead/WriteTrue to wrap text at the shape boundary.
is_lockedboolRead/WriteTrue to lock the shape when worksheet protection is active.
is_hiddenboolRead/WriteTrue to hide the shape.
hyperlinkstr | NoneRead/WriteURL or cell reference hyperlink attached to the shape.

Methods

copy

shape.copy() -> Shape

Returns a deep copy of this shape with all formatting preserved.


ShapeCollection

ShapeCollection is accessed as ws.shapes. It contains all shapes on a worksheet.

Methods

add

ws.shapes.add(
    drawing_type: MsoDrawingType,
    upper_left_row: int,
    upper_left_column: int,
    lower_right_row: int,
    lower_right_column: int
) -> Shape

Adds a shape of the specified type. All position arguments are 0-based row/column indices.

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

Shortcut for add(MsoDrawingType.TEXT_BOX, ...).

Properties and Iteration

MemberDescription
countNumber of shapes in the collection (read-only).
__len__()Returns count.
__iter__()Iterates over all Shape objects.
__getitem__(index)Returns the Shape at zero-based index.

Examples

Add a Rectangle with Solid Fill

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")

Add a Text Box

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")

See Also