Style

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, Protection

Properties

Properties

Style()

创建一个默认的 Style 未应用任何显式格式.

Properties

PropertiesPropertiesPropertiesProperties
fontFontProperties字体设置(名称、大小、颜色、粗体、斜体、下划线、删除线).
fillFillProperties单元格填充/背景设置(图案、前景色、背景色).
bordersBordersProperties单元格的所有边框设置(上、下、左、右、对角线).
alignmentAlignmentProperties文本对齐设置(水平、垂直、换行、缩进、旋转)。.
number_formatstr读/写活动的数字格式代码字符串(例如,., "#,##0.00").
protectionProtectionProperties单元格保护设置(锁定,公式隐藏)。.

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

PropertiesPropertiesProperties
namestr字体族名称(例如,., "Calibri", "Arial").
size`intfloat`
colorstr字体颜色使用 RRGGBB 十六进制字符串(例如,., "FF0000" 表示红色)。.
boldboolTrue 应用粗体字重。.
italicboolTrue 应用斜体样式。.
underlineboolTrue 应用单线下划线。.
strikethroughboolTrue 应用删除线。.

Properties

Properties

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

Properties

PropertiesPropertiesProperties
pattern_typestr填充图案类型(例如,., 'solid', 'none', 'darkGray').
foreground_colorstr前景色使用 RRGGBB 十六进制。.
background_colorstr背景色使用 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

PropertiesPropertiesProperties
line_stylestr边框线样式:: 'none', 'thin', 'medium', 'thick', 'dashed', 'dotted', 'double'.
colorstr边框颜色使用 RRGGBB 十六进制。.
weightint边框粗细(厚度)为整数。.

Properties

Properties

Borders()

一个包含四个单元格边框以及对角线边框的容器。.

Properties

PropertiesPropertiesProperties
topBorder顶部边框设置。.
bottomBorder底部边框设置。.
leftBorder左侧边框设置。.
rightBorder右侧边框设置。.
diagonalBorder对角线边框设置。.
diagonal_upboolTrue 绘制向上对角线。.
diagonal_downboolTrue 绘制向下对角线。.

Properties

Properties

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

Properties

PropertiesPropertiesProperties
horizontalstr水平对齐方式:: 'general', 'left', 'center', 'right', 'fill', 'justify', 'centerContinuous', 'distributed'.
verticalstr垂直对齐方式:: 'top', 'center', 'bottom', 'justify', 'distributed'.
wrap_textboolTrue 在单元格内自动换行文本。.
indentint缩进级别(0–15)。.
text_rotationint文本旋转角度(0–180 度,或 255 表示堆叠)。.
shrink_to_fitboolTrue 缩小文本以适应单元格。.

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

Properties True 如果给定的格式代码匹配已知的内置 Excel 格式,则返回。.

lookup_builtin_format

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

返回格式代码字符串对应的内置格式 ID,或 None 如果该代码不是内置格式。.


Properties

Properties

Protection(locked=True, hidden=False)

Properties

PropertiesPropertiesProperties
lockedboolTrue 表示在工作表保护开启时单元格被锁定(默认)。.
hiddenboolTrue 表示在工作表保护开启时,单元格中的任何公式在公式栏中被隐藏。.

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

另见

 中文