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, ProtectionEnumerations
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
| Enumerations | Enumerations | Enumerations | Enumerations |
|---|---|---|---|
font | Font | Enumerations | Cà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). |
fill | Fill | Enumerations | Cài đặt nền/đổ màu ô (mẫu, màu nền trước, màu nền sau). |
borders | Borders | Enumerations | Tất cả cài đặt viền cho ô (trên, dưới, trái, phải, chéo). |
alignment | Alignment | Enumerations | Cài đặt căn chỉnh văn bản (ngang, dọc, ngắt dòng, thụt lề, xoay). |
number_format | str | Đọc/Ghi | Chuỗi mã định dạng số đang hoạt động (ví dụ,., "#,##0.00"). |
protection | Protection | Enumerations | Cài đặt bảo vệ ô (khóa, công thức ẩn). |
Enumerations
copy
style.copy() -> StyleTrả 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
| Enumerations | Enumerations | Enumerations |
|---|---|---|
name | str | Tên họ phông chữ (ví dụ,., "Calibri", "Arial"). |
size | `int | float` |
color | str | Màu phông chữ dưới dạng chuỗi hex RRGGBB (ví dụ,., "FF0000" cho màu đỏ). |
bold | bool | True để áp dụng độ đậm. |
italic | bool | True để áp dụng kiểu chữ nghiêng. |
underline | bool | True để áp dụng gạch chân đơn. |
strikethrough | bool | True để áp dụng gạch ngang. |
Enumerations
Enumerations
Fill(pattern_type='none', foreground_color='FFFFFFFF', background_color='FFFFFFFF')Enumerations
| Enumerations | Enumerations | Enumerations |
|---|---|---|
pattern_type | str | Kiểu mẫu lấp đầy (ví dụ,., 'solid', 'none', 'darkGray'). |
foreground_color | str | Màu tiền cảnh dưới dạng thập lục phân RRGGBB. |
background_color | str | Mà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
| Enumerations | Enumerations | Enumerations |
|---|---|---|
line_style | str | Kiểu đường viền: 'none', 'thin', 'medium', 'thick', 'dashed', 'dotted', 'double'. |
color | str | Màu viền dưới dạng thập lục phân RRGGBB. |
weight | int | Độ 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
| Enumerations | Enumerations | Enumerations |
|---|---|---|
top | Border | Cài đặt viền trên. |
bottom | Border | Cài đặt viền dưới. |
left | Border | Cài đặt viền trái. |
right | Border | Cài đặt viền phải. |
diagonal | Border | Cài đặt viền chéo. |
diagonal_up | bool | True để vẽ đường chéo lên. |
diagonal_down | bool | True để 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
| Enumerations | Enumerations | Enumerations |
|---|---|---|
horizontal | str | Căn chỉnh ngang: 'general', 'left', 'center', 'right', 'fill', 'justify', 'centerContinuous', 'distributed'. |
vertical | str | Căn chỉnh dọc: 'top', 'center', 'bottom', 'justify', 'distributed'. |
wrap_text | bool | True để bọc văn bản trong ô. |
indent | int | Mức độ thụt lề (0–15). |
text_rotation | int | Xoay văn bản theo độ (0–180 hoặc 255 cho dạng xếp chồng). |
shrink_to_fit | bool | True để 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) -> strTrả 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) -> boolEnumerations 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 | NoneTrả 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
| Enumerations | Enumerations | Enumerations |
|---|---|---|
locked | bool | True có nghĩa là ô được khóa khi bảo vệ bảng tính được bật (mặc định). |
hidden | bool | True 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")