Shape — Aspose.Cells FOSS for Python API Reference

aspose-cells-foss hỗ trợ việc thêm các hình dạng hình học và hộp văn bản vào các bảng tính thông qua the ShapeCollection (ws.shapes). Mỗi hình dạng được biểu diễn bằng một Shape đối tượng có các thuộc tính fill, line và text có thể cấu hình.

Enumerations: aspose.cells_foss

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

MsoDrawingType

MsoDrawingType là một IntEnum xác định loại hình dạng hình học.

EnumerationsEnumerationsEnumerationsEnumerations
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 là một IntEnum xác định loại fill được áp dụng cho một hình dạng.

EnumerationsEnumerations
NONE0
SOLID1
GRADIENT2
PATTERN3
AUTOMATIC4

MsoLineDashStyle

MsoLineDashStyle là một IntEnum cho mẫu dash của border/outline line dash pattern.

EnumerationsEnumerations
SOLID0
DASH1
DOT2
DASH_DOT3
DASH_DOT_DOT4
LONG_DASH5
LONG_DASH_DOT6

TextAlignmentType

TextAlignmentType là một IntEnum cho căn chỉnh văn bản ngang bên trong một hình dạng.

EnumerationsEnumerations
LEFT0
CENTER1
RIGHT2
JUSTIFY3
DISTRIBUTED4

TextAnchorType

TextAnchorType là một IntEnum cho việc neo văn bản dọc bên trong một hình dạng.

EnumerationsEnumerations
TOP0
MIDDLE1
BOTTOM2
JUSTIFIED3

MsoFillFormat

Điều khiển giao diện fill của một hình dạng. Truy cập qua shape.fill.

Enumerations

EnumerationsEnumerationsEnumerations
fill_typeFillTypeLoại fill đang hoạt động.
fore_colorstrMàu fill nền (chính) dưới dạng hex RRGGBB.
transparencyfloatFill opacity là một giá trị từ 0.0 (hoàn toàn không trong suốt) đến 1.0 (hoàn toàn trong suốt).

Enumerations

copy

fill.copy() -> MsoFillFormat

Trả về một bản sao sâu của các cài đặt fill này.


MsoLineFormat

Kiểm soát giao diện viền/đường viền của một hình dạng. Truy cập qua shape.line.

Enumerations

EnumerationsEnumerationsEnumerations
is_visibleboolTrue để vẽ đường viền hình dạng.
colorstrMàu đường viền dưới dạng hex RRGGBB.
weightintĐộ dày đường viền (thickness) tính bằng điểm.
dash_styleMsoLineDashStyleMẫu gạch cho đường viền.
transparencyfloatĐộ trong suốt của đường viền từ 0.0 (đặc) đến 1.0 (trong suốt).

Enumerations

copy

line.copy() -> MsoLineFormat

Trả về một bản sao sâu của các cài đặt line này.


ShapeFont

Điều khiển phông chữ văn bản bên trong một hình dạng. Truy cập qua shape.font.

Enumerations

EnumerationsEnumerationsEnumerations
namestrTên họ phông chữ (ví dụ,., "Calibri").
sizefloatKích thước phông chữ tính bằng điểm.
boldboolTrue để in đậm.
italicboolTrue để in nghiêng.
underlineboolTrue để gạch chân đơn.
colorstrMàu phông chữ dưới dạng hex RRGGBB.

Enumerations

copy

font.copy() -> ShapeFont

Trả về một bản sao sâu của các cài đặt phông chữ này.


Enumerations

Shape các đối tượng không được tạo trực tiếp. Sử dụng ws.shapes.add(...) hoặc ws.shapes.add_text_box(...).

Enumerations

EnumerationsEnumerationsEnumerationsEnumerations
namestrĐọc/GhiTên hình (phải là duy nhất trong bảng tính).
drawing_typeMsoDrawingTypeEnumerationsKiểu hình học của hình này.
upper_left_rowintĐọc/Ghi0-based row index of the top-left anchor cell.
upper_left_columnintĐọc/Ghi0-based column index of the top-left anchor cell.
lower_right_rowintĐọc/Ghi0-based row index of the bottom-right anchor cell.
lower_right_columnintĐọc/Ghi0-based column index of the bottom-right anchor cell.
upper_left_row_offsetintĐọc/GhiKhoảng cách tính bằng pixel từ cạnh trên của ô trên‑trái.
upper_left_column_offsetintĐọc/GhiKhoảng cách tính bằng pixel từ cạnh trái của ô trên‑trái.
lower_right_row_offsetintĐọc/GhiKhoảng cách tính bằng pixel từ cạnh trên của ô dưới‑phải.
lower_right_column_offsetintĐọc/GhiKhoảng cách tính bằng pixel từ cạnh trái của ô dưới‑phải.
fillMsoFillFormatEnumerationsCài đặt tô (nền).
lineMsoLineFormatEnumerationsCài đặt viền/đường viền.
textstrĐọc/GhiNội dung văn bản hiển thị bên trong hình dạng.
fontShapeFontEnumerationsCài đặt phông chữ cho văn bản hình dạng.
text_horizontal_alignmentTextAlignmentTypeĐọc/GhiCăn chỉnh văn bản theo chiều ngang bên trong hình dạng.
text_vertical_alignmentTextAnchorTypeĐọc/GhiNeo văn bản dọc bên trong hình dạng.
is_text_wrappedboolĐọc/ViếtTrue để bọc văn bản tại ranh giới của hình dạng.
is_lockedboolĐọc/ViếtTrue để khóa hình dạng khi bảo vệ bảng tính đang hoạt động.
is_hiddenboolĐọc/ViếtTrue để ẩn hình dạng.
hyperlink`strNone`Đọc/Viết

Enumerations

copy

shape.copy() -> Shape

Trả về một bản sao sâu của hình dạng này với mọi định dạng được bảo toàn.


ShapeCollection

ShapeCollection được truy cập như ws.shapes. Nó chứa tất cả các hình dạng trên một bảng tính.

Enumerations

add

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

Thêm một hình dạng theo loại được chỉ định. Tất cả các đối số vị trí đều là chỉ số hàng/cột bắt đầu từ 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

Phím tắt cho add(MsoDrawingType.TEXT_BOX, ...).

Thuộc tính và Lặp lại

EnumerationsEnumerations
countSố lượng hình dạng trong bộ sưu tập (chỉ đọc).
__len__()Enumerations count.
__iter__()Lặp lại qua tất cả Shape đối tượng.
__getitem__(index)Trả về Shape với chỉ số bắt đầu từ 0 index.

Enumerations

Thêm một Rectangle với 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")

Thêm một 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")

Xem Thêm

 Tiếng Việt