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
คัดลอก
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
| 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 แบบ hex. |
background_color | str | สีพื้นหลังเป็นรหัส 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
| Properties | Properties | Properties |
|---|---|---|
line_style | str | สไตล์เส้นขอบ: 'none', 'thin', 'medium', 'thick', 'dashed', 'dotted', 'double'. |
color | str | สีขอบเป็นรหัส RRGGBB แบบ hex. |
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")