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

คัดลอก

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)

ใช้รูปแบบตัวเลขที่มีใน Excel หนึ่งรูปแบบโดยใช้ ID จำนวนเต็มของมัน (เช่น,., 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 แบบ hex.
background_colorstrสีพื้นหลังเป็นรหัส RRGGBB แบบ hex.

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 แบบ hex.
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")

ดูเพิ่มเติม

 ภาษาไทย