Style — Aspose.Cells FOSS for Python API Reference
Properties Style 类及其伴生类型控制在 aspose-cells-foss 中应用于单元格的所有视觉和保护格式。A Style 对象通过以下方式从单元格获取 cell.get_style() 并使用以下方式重新应用 cell.apply_style(style).
Properties: aspose.cells_foss
from aspose.cells_foss import Style, Font, Fill, Border, Borders, Alignment, NumberFormat, ProtectionProperties
Properties
Style()创建一个默认的 Style 未应用任何显式格式.
Properties
| Properties | Properties | Properties | Properties |
|---|---|---|---|
font | Font | Properties | 字体设置(名称、大小、颜色、粗体、斜体、下划线、删除线). |
fill | Fill | Properties | 单元格填充/背景设置(图案、前景色、背景色). |
borders | Borders | Properties | 单元格的所有边框设置(上、下、左、右、对角线). |
alignment | Alignment | Properties | 文本对齐设置(水平、垂直、换行、缩进、旋转)。. |
number_format | str | 读/写 | 活动的数字格式代码字符串(例如,., "#,##0.00"). |
protection | Protection | Properties | 单元格保护设置(锁定,公式隐藏)。. |
Properties
copy
style.copy() -> Style返回此样式对象的深拷贝。.
set_fill_color
style.set_fill_color(color: str)设置纯色填充背景颜色。. color 是一个 RRGGBB 十六进制字符串(例如,., "FFFF00" 用于黄色)。.
set_fill_pattern
style.set_fill_pattern(pattern_type: str, fg_color: str = 'FFFFFFFF', bg_color: str = 'FFFFFFFF')设置图案填充。. pattern_type 值包括 '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()清除所有填充,将单元格背景设为透明(无填充)。.
set_border_color
style.set_border_color(side: str, color: str)设置单个边框侧面的颜色。. side 必须是以下之一 'top', 'bottom', 'left', 'right', 'diagonal'. color 是一个 RRGGBB 十六进制字符串。.
set_border_style
style.set_border_style(side: str, style: str)设置边框侧的线条样式。. style 值:: 'none', 'thin', 'medium', 'thick', 'dashed', 'dotted', 'double'.
set_border_weight
style.set_border_weight(side: str, weight: int)将边框一侧的线宽(粗细)设置为整数。.
set_border
style.set_border(side: str, line_style='none', color='FF000000', weight=1)便利方法,可在一次调用中为单个边框侧设置线型、颜色和粗细。.
set_diagonal_border
style.set_diagonal_border(line_style='none', color='FF000000', weight=1, up=False, down=False)设置对角线边框。传入 up=True 表示向上对角线,, down=True 表示向下对角线,或两者皆传以绘制十字。.
set_horizontal_alignment
style.set_horizontal_alignment(alignment: str)设置水平文本对齐方式。值:: 'general', 'left', 'center', 'right', 'fill', 'justify', 'centerContinuous', 'distributed'.
set_vertical_alignment
style.set_vertical_alignment(alignment: str)设置垂直文本对齐方式。值:: 'top', 'center', 'bottom', 'justify', 'distributed'.
set_text_wrap
style.set_text_wrap(wrap=True)启用或禁用单元格内的文本换行。.
set_shrink_to_fit
style.set_shrink_to_fit(shrink=True)启用或禁用自动缩小以适应。启用后,文本会缩小以适应单元格宽度。.
set_indent
style.set_indent(indent: int)设置缩进级别(0–15)。.
set_text_rotation
style.set_text_rotation(rotation: int)设置文本旋转角度(单位:度)。有效值为 0–180(相对于 0° 的逆时针方向)或 255(垂直文本从上到下堆叠)。.
set_reading_order
style.set_reading_order(order: int)设置阅读顺序。. 0 = 取决于上下文,, 1 = 从左到右,, 2 = 从右到左。.
set_number_format
style.set_number_format(format_code: str)应用自定义数字格式字符串(例如,., "DD/MM/YYYY", "#,##0.00 [$€-407]").
set_builtin_number_format
style.set_builtin_number_format(format_id: int)通过整数 ID 应用 Excel 内置的数字格式之一(例如,., 9 = "0%", 14 = "m/d/yy").
set_locked
style.set_locked(locked=True)将单元格标记为锁定状态。工作表受保护时,锁定的单元格无法编辑。.
set_formula_hidden
style.set_formula_hidden(hidden=True)在工作表受保护时,将公式隐藏于公式栏中。.
Properties
Properties
Font(name='Calibri', size=11, color='FF000000', bold=False, italic=False, underline=False, strikethrough=False)Properties
| Properties | Properties | Properties |
|---|---|---|
name | str | 字体族名称(例如,., "Calibri", "Arial"). |
size | `int | float` |
color | str | 字体颜色使用 RRGGBB 十六进制字符串(例如,., "FF0000" 表示红色)。. |
bold | bool | True 应用粗体字重。. |
italic | bool | True 应用斜体样式。. |
underline | bool | True 应用单线下划线。. |
strikethrough | bool | True 应用删除线。. |
Properties
Properties
Fill(pattern_type='none', foreground_color='FFFFFFFF', background_color='FFFFFFFF')Properties
| Properties | Properties | Properties |
|---|---|---|
pattern_type | str | 填充图案类型(例如,., 'solid', 'none', 'darkGray'). |
foreground_color | str | 前景色使用 RRGGBB 十六进制。. |
background_color | str | 背景色使用 RRGGBB 十六进制。. |
Properties
set_solid_fill
fill.set_solid_fill(color: str)使用给定的 RRGGBB 颜色应用纯色填充。.
set_gradient_fill
fill.set_gradient_fill(start_color: str, end_color: str)应用双颜色渐变填充。.
set_pattern_fill
fill.set_pattern_fill(pattern_type: str, fg_color: str, bg_color: str)应用具有明确前景色和背景色的图案填充。.
set_no_fill
fill.set_no_fill()移除单元格中的所有填充。.
Properties
Properties
Border(line_style='none', color='FF000000', weight=1)Properties
| Properties | Properties | Properties |
|---|---|---|
line_style | str | 边框线样式:: 'none', 'thin', 'medium', 'thick', 'dashed', 'dotted', 'double'. |
color | str | 边框颜色使用 RRGGBB 十六进制。. |
weight | int | 边框粗细(厚度)为整数。. |
Properties
Properties
Borders()一个包含四个单元格边框以及对角线边框的容器。.
Properties
| Properties | Properties | Properties |
|---|---|---|
top | Border | 顶部边框设置。. |
bottom | Border | 底部边框设置。. |
left | Border | 左侧边框设置。. |
right | Border | 右侧边框设置。. |
diagonal | Border | 对角线边框设置。. |
diagonal_up | bool | True 绘制向上对角线。. |
diagonal_down | bool | True 绘制向下对角线。. |
Properties
Properties
Alignment(horizontal='general', vertical='bottom', wrap_text=False, indent=0, text_rotation=0, shrink_to_fit=False)Properties
| Properties | Properties | Properties |
|---|---|---|
horizontal | str | 水平对齐方式:: 'general', 'left', 'center', 'right', 'fill', 'justify', 'centerContinuous', 'distributed'. |
vertical | str | 垂直对齐方式:: 'top', 'center', 'bottom', 'justify', 'distributed'. |
wrap_text | bool | True 在单元格内自动换行文本。. |
indent | int | 缩进级别(0–15)。. |
text_rotation | int | 文本旋转角度(0–180 度,或 255 表示堆叠)。. |
shrink_to_fit | bool | True 缩小文本以适应单元格。. |
NumberFormat
NumberFormat 是仅包含静态方法的工具类,不能直接实例化。.
静态方法
get_builtin_format
NumberFormat.get_builtin_format(format_id: int) -> str返回给定内置格式 ID 的格式代码字符串(例如,., NumberFormat.get_builtin_format(9) 返回 "0%").
is_builtin_format
NumberFormat.is_builtin_format(format_code: str) -> boolProperties True 如果给定的格式代码匹配已知的内置 Excel 格式,则返回。.
lookup_builtin_format
NumberFormat.lookup_builtin_format(format_code: str) -> int | None返回格式代码字符串对应的内置格式 ID,或 None 如果该代码不是内置格式。.
Properties
Properties
Protection(locked=True, hidden=False)Properties
| Properties | Properties | Properties |
|---|---|---|
locked | bool | True 表示在工作表保护开启时单元格被锁定(默认)。. |
hidden | bool | True 表示在工作表保护开启时,单元格中的任何公式在公式栏中被隐藏。. |
Properties
下面的示例为标题单元格设置粗体红色文字、黄色背景以及四边细黑色边框。.
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")