إعدادات التعبئة
الألوان وأنماط الخلفية
يمكن لبرنامج Microsoft Excel تعيين ألوان الأمام (الإطار) والخلفية (تعبئة) للخلايا وأنماط الخلفية.
يدعم Aspose.Cells لبايثون via .NET أيضًا هذه الميزات بطريقة مرنة. في هذا الموضوع، نتعلم كيف نستخدم هذه الميزات باستخدام Aspose.Cells.
تعيين الألوان وأنماط الخلفية
يوفر Aspose.Cells لبايثون via .NET فئة، Workbook، تمثل ملف Microsoft Excel. تحتوي الفئة Workbook على مجموعة worksheets تتيح الوصول إلى كل ورقة عمل في ملف Excel. تمثل ورقة العمل بواسطة الفئة Worksheet. توفر الفئة Worksheet مجموعة Cells. كل عنصر في مجموعة Cells يمثل كائن من فئة Cell.
تحتوي Cell على طريقتي get_style و set_style التي تُستخدم للحصول على وتعيين تنسيق الخلية. توفر فئة Style خصائص لضبط ألوان المقدمة والخلفية للخلايا. تقدم Aspose.Cells for Python via .NET تعدادًا BackgroundType يحتوي على مجموعة من الأنماط الخلفية المعرفة مسبقًا والتي سيتم توضيحها أدناه.
أنماط الخلفية | الوصف |
---|---|
DIAGONAL_CROSSHATCH | يمثل نمط متقاطع قطري |
DIAGONAL_STRIPE | يمثل نمط شريط مائل قطري |
GRAY6 | يمثل نمط رمادي بنسبة 6.25% |
GRAY12 | يمثل نمط رمادي بنسبة 12.5% |
GRAY25 | يمثل نمط رمادي بنسبة 25% |
GRAY50 | يمثل نمط رمادي بنسبة 50% |
GRAY75 | يمثل نمط رمادي بنسبة 75% |
HORIZONTAL_STRIPE | يمثل نمط شريط أفقي |
NONE | لا يمثل خلفية |
REVERSE_DIAGONAL_STRIPE | يمثل نمط شريط قطري عكسي |
SOLID | يمثل نمط صلب |
THICK_DIAGONAL_CROSSHATCH | يمثل نمط متقاطع قطري بسماكة عالية |
THIN_DIAGONAL_CROSSHATCH | يمثل نمط متقاطع قطري رقيق |
THIN_DIAGONAL_STRIPE | يمثل نمط شريط قطري رقيق |
THIN_HORIZONTAL_CROSSHATCH | يمثل نمط متقاطع أفقي رقيق |
THIN_HORIZONTAL_STRIPE | يمثل نمط شريط أفقي رقيق |
THIN_REVERSE_DIAGONAL_STRIPE | يمثل نمط شريط قطري عكسي رقيق |
THIN_VERTICAL_STRIPE | يمثل نمط شريط عمودي رقيق |
VERTICAL_STRIPE | يمثل نمط شريط عمودي |
في المثال أدناه ، تم تعيين لون الخلفية للخلية A1 ولكن تم تكوين A2 ليكون لها كل من لون الخلفية والأمامية مع نمط خلفية خط عمودي.
from aspose.cells import BackgroundType, SaveFormat, Workbook | |
from aspose.pydrawing import Color | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Define a Style and get the A1 cell style | |
style = worksheet.cells.get("A1").get_style() | |
# Setting the foreground color to yellow | |
style.foreground_color = Color.yellow | |
# Setting the background pattern to vertical stripe | |
style.pattern = BackgroundType.VERTICAL_STRIPE | |
# Apply the style to A1 cell | |
worksheet.cells.get("A1").set_style(style) | |
# Get the A2 cell style | |
style = worksheet.cells.get("A2").get_style() | |
# Setting the foreground color to blue | |
style.foreground_color = Color.blue | |
# Setting the background color to yellow | |
style.background_color = Color.yellow | |
# Setting the background pattern to vertical stripe | |
style.pattern = BackgroundType.VERTICAL_STRIPE | |
# Apply the style to A2 cell | |
worksheet.cells.get("A2").set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003) |
مهم معرفته
- لتعيين لون الخلفية أو اللون الأمامي لخلية ما، استخدم خصائص foreground_color أو background_color لكائن Style. سوف تؤثر كلا الخصائص فقط إذا تم تكوين خصية pattern لكائن Style.
- تعيين الظل للخلية تحدد خاصية foreground_color لون الخلية. تحدد الخاصية pattern نوع النمط الخلفي المستخدم لللون الأمامي أو الخلفي. تقدم Aspose.Cells for Python via .NET تعدادًا BackgroundType يحتوي على مجموعة من أنواع الأنماط الخلفية المعرفة مسبقًا.
- إذا قمت بتحديد قيمة BackgroundType.None من تعداد BackgroundType، لن يتم تطبيق اللون الأمامي. بالمثل، لن يتم تطبيق اللون الخلفي إذا قمت باختيار القيم BackgroundType.None أو BackgroundType.Solid.
- عند استرجاع لون السطوع/التعبئة للخلية، إذا كان Style.pattern يساوي BackgroundType.None، سيقوم Style.foreground_color بإرجاع Color.Empty.
تطبيق تأثيرات تعبئة التدرج
لتطبيق تأثيرات تعبئة التدرج المرغوبة على الخلية، استخدم الطريقة set_two_color_gradient لكائن Style وفقًا لذلك.
from aspose.cells import TextAlignmentType, Workbook | |
from aspose.cells.drawing import GradientStyleType | |
from aspose.pydrawing import Color | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiate a new Workbook | |
workbook = Workbook() | |
# Get the first worksheet (default) in the workbook | |
worksheet = workbook.worksheets[0] | |
# Input a value into B3 cell | |
worksheet.cells.get(2, 1).put_value("test") | |
# Get the Style of the cell | |
style = worksheet.cells.get("B3").get_style() | |
# Set Gradient pattern on | |
style.is_gradient = True | |
# Specify two color gradient fill effects | |
style.set_two_color_gradient(Color.from_argb(255, 255, 255), Color.from_argb(79, 129, 189), GradientStyleType.HORIZONTAL, 1) | |
# Set the color of the text in the cell | |
style.font.color = Color.red | |
# Specify horizontal and vertical alignment settings | |
style.horizontal_alignment = TextAlignmentType.CENTER | |
style.vertical_alignment = TextAlignmentType.CENTER | |
# Apply the style to the cell | |
worksheet.cells.get("B3").set_style(style) | |
# Set the third row height in pixels | |
worksheet.cells.set_row_height_pixel(2, 53) | |
# Merge the range of cells (B3:C3) | |
worksheet.cells.merge(2, 1, 1, 2) | |
# Save the Excel file | |
workbook.save(dataDir + "output.xlsx") |
الألوان واللوحة
اللوحة هي عدد الألوان المتاحة للاستخدام في إنشاء صورة. يتيح استخدام لوحة معيارية في العرض للمستخدم إنشاء مظهر متسق. كل ملف من ملفات Microsoft Excel (97-2003) لديه لوحة تتكون من 56 لون يمكن تطبيقها على الخلايا، الخطوط، الخطوط الشبكية، الكائنات الرسومية، التعبئات والخطوط في الرسم البياني.
مع Aspose.Cells for Python via .NET لا يقتصر الأمر على استخدام ألوان اللوحة الحالية ولكن أيضًا يمكن استخدام الألوان المخصصة. قبل استخدام لون مخصص، أضفه إلى اللوحة أولاً.
يناقش هذا الموضوع كيفية إضافة ألوان مخصصة إلى اللوحة.
إضافة ألوان مخصصة إلى اللوحة
يدعم Aspose.Cells for Python via .NET لوحة ألوان مايكروسوفت إكسل التي تتألف من 56 لونًا. لاستخدام لون مخصص غير معرف في اللوحة، أضف اللون إلى اللوحة.
توفر Aspose.Cells for Python via .NET فئة Workbook، التي تمثل ملف إكسل من مايكروسوفت. توفر الفئة Workbook طريقة change_palette التي تتطلب المعلمات التالية لإضافة لون مخصص لتعديل اللوحة:
- لون مخصص، اللون المخصص الذي سيتم إضافته.
- الفهرس، فهرس اللون في اللوحة الذي سيحل محل اللون المخصص. يجب أن يكون بين 0-55.
المثال أدناه يضيف لون مخصص (Orchid) إلى اللوحة قبل تطبيقه على خط النص.
from aspose.cells import SaveFormat, Workbook | |
from aspose.pydrawing import Color | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating an Workbook object | |
workbook = Workbook() | |
# Adding Orchid color to the palette at 55th index | |
workbook.change_palette(Color.orchid, 55) | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Defining new Style object | |
styleObject = workbook.create_style() | |
# Setting the Orchid (custom) color to the font | |
styleObject.font.color = Color.orchid | |
# Applying the style to the cell | |
cell.set_style(styleObject) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.AUTO) |