Style — Aspose.Cells FOSS for Python API Reference

Enumerations Style lớp và các kiểu đồng hành của nó kiểm soát tất cả định dạng trực quan và bảo vệ được áp dụng cho các ô trong aspose-cells-foss. A Style đối tượng được lấy từ một ô thông qua cell.get_style() và được áp dụng lại bằng cell.apply_style(style).

Enumerations: aspose.cells_foss

from aspose.cells_foss import Style, Font, Fill, Border, Borders, Alignment, NumberFormat, Protection

Enumerations

Enumerations

Style()

Tạo một mặc định Style không có định dạng nào được áp dụng một cách rõ ràng.

Enumerations

EnumerationsEnumerationsEnumerationsEnumerations
fontFontEnumerationsCài đặt phông chữ (tên, kích thước, màu, in đậm, in nghiêng, gạch dưới, gạch ngang).
fillFillEnumerationsCài đặt nền/đổ màu ô (mẫu, màu nền trước, màu nền sau).
bordersBordersEnumerationsTất cả cài đặt viền cho ô (trên, dưới, trái, phải, chéo).
alignmentAlignmentEnumerationsCài đặt căn chỉnh văn bản (ngang, dọc, ngắt dòng, thụt lề, xoay).
number_formatstrĐọc/GhiChuỗi mã định dạng số đang hoạt động (ví dụ,., "#,##0.00").
protectionProtectionEnumerationsCài đặt bảo vệ ô (khóa, công thức ẩn).

Enumerations

copy

style.copy() -> Style

Trả về một bản sao sâu của đối tượng style này.

set_fill_color

style.set_fill_color(color: str)

Đặt màu nền đổ đặc. color là một chuỗi hex RRGGBB (ví dụ,., "FFFF00" cho màu vàng).

set_fill_pattern

style.set_fill_pattern(pattern_type: str, fg_color: str = 'FFFFFFFF', bg_color: str = 'FFFFFFFF')

Đặt một nền có mẫu. pattern_type các giá trị bao gồm 'solid', 'darkGray', 'mediumGray', 'lightGray', 'gray125', 'gray0625', 'darkHorizontal', 'darkVertical', 'darkDown', 'darkUp', 'darkGrid', 'darkTrellis', 'lightHorizontal', 'lightVertical', 'lightDown', 'lightUp', 'lightGrid', 'lightTrellis'.

set_no_fill

style.set_no_fill()

Xóa mọi nền, đặt nền ô thành trong suốt (không có nền).

set_border_color

style.set_border_color(side: str, color: str)

Đặt màu cho một phía viền duy nhất. side phải là một trong 'top', 'bottom', 'left', 'right', 'diagonal'. color là một chuỗi hex RRGGBB.

set_border_style

style.set_border_style(side: str, style: str)

Đặt kiểu đường cho một cạnh viền. style giá trị: 'none', 'thin', 'medium', 'thick', 'dashed', 'dotted', 'double'.

set_border_weight

style.set_border_weight(side: str, weight: int)

Đặt độ dày (độ rộng) của một cạnh viền dưới dạng số nguyên.

set_border

style.set_border(side: str, line_style='none', color='FF000000', weight=1)

Phương thức tiện lợi thiết lập kiểu đường, màu và độ dày cho một cạnh viền duy nhất trong một lần gọi.

set_diagonal_border

style.set_diagonal_border(line_style='none', color='FF000000', weight=1, up=False, down=False)

Đặt viền chéo. Truyền up=True để tạo đường chéo lên trên, down=True để tạo đường chéo xuống dưới, hoặc cả hai để vẽ chéo giao nhau.

set_horizontal_alignment

style.set_horizontal_alignment(alignment: str)

Đặt căn chỉnh văn bản theo chiều ngang. Giá trị: 'general', 'left', 'center', 'right', 'fill', 'justify', 'centerContinuous', 'distributed'.

set_vertical_alignment

style.set_vertical_alignment(alignment: str)

Đặt căn chỉnh văn bản theo chiều dọc. Giá trị: 'top', 'center', 'bottom', 'justify', 'distributed'.

set_text_wrap

style.set_text_wrap(wrap=True)

Bật hoặc tắt việc ngắt dòng văn bản trong ô.

set_shrink_to_fit

style.set_shrink_to_fit(shrink=True)

Bật hoặc tắt tính năng thu nhỏ để vừa. Khi bật, văn bản sẽ được thu nhỏ để vừa với chiều rộng của ô.

set_indent

style.set_indent(indent: int)

Đặt mức độ thụt lề (0–15).

set_text_rotation

style.set_text_rotation(rotation: int)

Đặt góc quay của văn bản tính bằng độ. Các giá trị hợp lệ là 0–180 (ngược chiều kim đồng hồ từ 0°) hoặc 255 (văn bản dọc xếp từ trên xuống dưới).

set_reading_order

style.set_reading_order(order: int)

Đặt thứ tự đọc. 0 = phụ thuộc vào ngữ cảnh, 1 = từ trái sang phải, 2 = từ phải sang trái.

set_number_format

style.set_number_format(format_code: str)

Áp dụng chuỗi định dạng số tùy chỉnh (ví dụ,., "DD/MM/YYYY", "#,##0.00 [$€-407]").

set_builtin_number_format

style.set_builtin_number_format(format_id: int)

Áp dụng một trong các định dạng số có sẵn của Excel bằng ID nguyên của nó (ví dụ,., 9 = "0%", 14 = "m/d/yy").

set_locked

style.set_locked(locked=True)

Đánh dấu ô là bị khóa. Các ô bị khóa không thể chỉnh sửa khi bảo vệ bảng tính đang hoạt động.

set_formula_hidden

style.set_formula_hidden(hidden=True)

Ẩn công thức khỏi thanh công thức khi bảo vệ bảng tính đang hoạt động.


Enumerations

Enumerations

Font(name='Calibri', size=11, color='FF000000', bold=False, italic=False, underline=False, strikethrough=False)

Enumerations

EnumerationsEnumerationsEnumerations
namestrTên họ phông chữ (ví dụ,., "Calibri", "Arial").
size`intfloat`
colorstrMàu phông chữ dưới dạng chuỗi hex RRGGBB (ví dụ,., "FF0000" cho màu đỏ).
boldboolTrue để áp dụng độ đậm.
italicboolTrue để áp dụng kiểu chữ nghiêng.
underlineboolTrue để áp dụng gạch chân đơn.
strikethroughboolTrue để áp dụng gạch ngang.

Enumerations

Enumerations

Fill(pattern_type='none', foreground_color='FFFFFFFF', background_color='FFFFFFFF')

Enumerations

EnumerationsEnumerationsEnumerations
pattern_typestrKiểu mẫu lấp đầy (ví dụ,., 'solid', 'none', 'darkGray').
foreground_colorstrMàu tiền cảnh dưới dạng thập lục phân RRGGBB.
background_colorstrMàu nền dưới dạng thập lục phân RRGGBB.

Enumerations

set_solid_fill

fill.set_solid_fill(color: str)

Áp dụng màu nền đặc với màu RRGGBB được cung cấp.

set_gradient_fill

fill.set_gradient_fill(start_color: str, end_color: str)

Áp dụng nền gradient hai màu.

set_pattern_fill

fill.set_pattern_fill(pattern_type: str, fg_color: str, bg_color: str)

Áp dụng màu nền có họa tiết với màu nền trước và nền sau được chỉ định rõ ràng.

set_no_fill

fill.set_no_fill()

Xóa mọi màu nền khỏi ô.


Enumerations

Enumerations

Border(line_style='none', color='FF000000', weight=1)

Enumerations

EnumerationsEnumerationsEnumerations
line_stylestrKiểu đường viền: 'none', 'thin', 'medium', 'thick', 'dashed', 'dotted', 'double'.
colorstrMàu viền dưới dạng thập lục phân RRGGBB.
weightintĐộ dày viền (độ dày) dưới dạng số nguyên.

Enumerations

Enumerations

Borders()

Một container cho cả bốn đường viền ô và các đường viền chéo.

Enumerations

EnumerationsEnumerationsEnumerations
topBorderCài đặt viền trên.
bottomBorderCài đặt viền dưới.
leftBorderCài đặt viền trái.
rightBorderCài đặt viền phải.
diagonalBorderCài đặt viền chéo.
diagonal_upboolTrue để vẽ đường chéo lên.
diagonal_downboolTrue để vẽ đường chéo xuống.

Enumerations

Enumerations

Alignment(horizontal='general', vertical='bottom', wrap_text=False, indent=0, text_rotation=0, shrink_to_fit=False)

Enumerations

EnumerationsEnumerationsEnumerations
horizontalstrCăn chỉnh ngang: 'general', 'left', 'center', 'right', 'fill', 'justify', 'centerContinuous', 'distributed'.
verticalstrCăn chỉnh dọc: 'top', 'center', 'bottom', 'justify', 'distributed'.
wrap_textboolTrue để bọc văn bản trong ô.
indentintMức độ thụt lề (0–15).
text_rotationintXoay văn bản theo độ (0–180 hoặc 255 cho dạng xếp chồng).
shrink_to_fitboolTrue để thu nhỏ văn bản cho vừa với ô.

NumberFormat

NumberFormat là một lớp tiện ích chỉ có các phương thức tĩnh. Nó không được khởi tạo trực tiếp.

Phương thức tĩnh

get_builtin_format

NumberFormat.get_builtin_format(format_id: int) -> str

Trả về chuỗi mã định dạng cho ID định dạng tích hợp sẵn đã cho (ví dụ,., NumberFormat.get_builtin_format(9) trả về "0%").

is_builtin_format

NumberFormat.is_builtin_format(format_code: str) -> bool

Enumerations True nếu mã định dạng đã cho khớp với một định dạng Excel tích hợp sẵn đã biết.

lookup_builtin_format

NumberFormat.lookup_builtin_format(format_code: str) -> int | None

Trả về ID định dạng tích hợp sẵn cho một chuỗi mã định dạng, hoặc None nếu mã không phải là định dạng tích hợp sẵn.


Enumerations

Enumerations

Protection(locked=True, hidden=False)

Enumerations

EnumerationsEnumerationsEnumerations
lockedboolTrue có nghĩa là ô được khóa khi bảo vệ bảng tính được bật (mặc định).
hiddenboolTrue có nghĩa là bất kỳ công thức nào trong ô sẽ bị ẩn trong thanh công thức khi bảo vệ bảng tính được bật.

Enumerations

Ví dụ sau tạo kiểu cho ô tiêu đề với văn bản in đậm màu đỏ, nền màu vàng, và viền mỏng màu đen ở mọi phía.

from aspose.cells_foss import Workbook

wb = Workbook()
ws = wb.worksheets[0]

ws.cells["A1"].put_value("Total Revenue")

style = ws.cells["A1"].get_style()

# Bold red font
style.font.bold = True
style.font.color = "FF0000"   # red
style.font.size = 13

# Yellow solid fill
style.set_fill_color("FFFF00")

# Thin black border on all four sides
for side in ("top", "bottom", "left", "right"):
    style.set_border(side, line_style="thin", color="000000", weight=1)

# Center the text
style.set_horizontal_alignment("center")

ws.cells["A1"].apply_style(style)

wb.save("styled.xlsx")

Xem Thêm

 Tiếng Việt