تعيين تنسيقات مشروطة للملفات Excel وODS.
مقدمة
تعتبر التنسيق الشرطي ميزة متقدمة في Microsoft Excel تسمح لك بتطبيق تنسيقات على خلية أو مجموعة من الخلايا وجعل تنسيق ذلك يتغير اعتمادًا على قيمة الخلية أو قيمة صيغة. على سبيل المثال، يمكنك جعل الخلية تظهر بخط عريض فقط عندما تكون قيمة الخلية أكبر من 500. عندما تفي قيمة الخلية بالشرط، يتم تطبيق التنسيق المحدد على الخلية. إذا لم تفي قيمة الخلية بشرط التنسيق، يتم استخدام التنسيق الافتراضي للخلية. في Microsoft Excel، حدد تنسيق ثم التنسيق الشرطي لفتح مربع حوار التنسيق الشرطي.
يدعم Aspose.Cells لبايثون via .NET تطبيق التنسيق الشرطي على الخلايا أثناء التشغيل. يوضح هذا المقال كيف. كما يشرح كيفية حساب اللون المستخدم بواسطة إكسل للتنسيق الشرطي لدرجة اللون.
تطبيق التنسيق الشرطي
يدعم Aspose.Cells لبايثون via .NET التنسيق الشرطي بعدة طرق:
- باستخدام جدول البيانات للمصمم
- باستخدام طريقة النسخ.
- إنشاء التنسيق الشرطي أثناء التشغيل.
استخدام جدول البيانات للمصمم
يمكن للمطورين إنشاء جدول تصميمي يتضمن تنسيقًا شرطيًا في Microsoft Excel ثم فتح ذلك الملف باستخدام Aspose.Cells لبايثون via .NET. يقوم Aspose.Cells لبايثون via .NET بتحميل وحفظ الملف التصميمي، مع الاحتفاظ بأي إعدادات للتنسيق الشرطي.
استخدام طريقة النسخ
يسمح Aspose.Cells لبايثون via .NET للمطورين بنسخ إعدادات التنسيق الشرطي من خلية إلى أخرى في ورقة العمل عن طريق استدعاء وظيفة Range.copy().
from aspose.cells import Workbook | |
# 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(".") | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(dataDir + "Book1.xlsx", "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Copying conditional format settings from cell "A1" to cell "B1" | |
# worksheet.CopyConditionalFormatting(0, 0, 0, 1); | |
TotalRowCount = 0 | |
for i in range(len(workbook.worksheets)): | |
sourceSheet = workbook.worksheets[i] | |
sourceRange = sourceSheet.cells.max_display_range | |
destRange = worksheet.cells.create_range(sourceRange.first_row + TotalRowCount, sourceRange.first_column, sourceRange.row_count, sourceRange.column_count) | |
destRange.copy(sourceRange) | |
TotalRowCount = sourceRange.row_count + TotalRowCount | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
تطبيق التنسيق الشرطي أثناء التشغيل
يمكنك Aspose.Cells لبايثون via .NET من إضافة وإزالة التنسيق الشرطي أثناء التشغيل. تعرض العينات البرمجية أدناه كيفية تعيين التنسيق الشرطي:
- قم بإنشاء ورقة العمل.
- أضف تنسيق شرطي فارغ.
- حدد النطاق الذي ينبغي تطبيق التنسيق عليه.
- حدد شروط التنسيق.
- حفظ الملف.
بعد هذا المثال يأتي عدد من الأمثلة الصغيرة الأخرى التي توضح كيفية تطبيق إعدادات الخط، إعدادات الحدود، وأنماط.
أضاف Microsoft Excel 2007 تنسيقًا شرطياً أكثر تقدمًا ويدعمه Aspose.Cells لبايثون via .NET أيضًا. توضح الأمثلة هنا كيفية استخدام التنسيق البسيط، وتوضح أمثلة Microsoft Excel 2007 كيفية تطبيق تنسيق شرطي أكثر تقدمًا.
from aspose.cells import CellArea, FormatConditionType, OperatorType, Workbook | |
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(".") | |
filePath = dataDir + "Book1.xlsx" | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
sheet = workbook.worksheets[0] | |
# Adds an empty conditional formatting | |
index = sheet.conditional_formattings.add() | |
fcs = sheet.conditional_formattings[index] | |
# Sets the conditional format range. | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 0 | |
ca.start_column = 0 | |
ca.end_column = 0 | |
fcs.add_area(ca) | |
ca = CellArea() | |
ca.start_row = 1 | |
ca.end_row = 1 | |
ca.start_column = 1 | |
ca.end_column = 1 | |
fcs.add_area(ca) | |
# Adds condition. | |
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "=A2", "100") | |
# Adds condition. | |
conditionIndex2 = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100") | |
# Sets the background color. | |
fc = fcs[conditionIndex] | |
fc.style.background_color = Color.red | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
تعيين الخط
from aspose.cells import SaveFormat, Workbook | |
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 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!") | |
# Obtaining the style of the cell | |
style = cell.get_style() | |
# Setting the font weight to bold | |
style.font.is_bold = True | |
# Applying the style to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003) |
تعيين الحدود
from aspose.cells import BorderType, CellArea, CellBorderType, FormatConditionType, OperatorType, Workbook | |
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(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
sheet = workbook.worksheets[0] | |
# Adds an empty conditional formatting | |
index = sheet.conditional_formattings.add() | |
fcs = sheet.conditional_formattings[index] | |
# Sets the conditional format range. | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 5 | |
ca.start_column = 0 | |
ca.end_column = 3 | |
fcs.add_area(ca) | |
# Adds condition. | |
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100") | |
# Sets the background color. | |
fc = fcs[conditionIndex] | |
fc.style.borders.get(BorderType.LEFT_BORDER).line_style = CellBorderType.DASHED | |
fc.style.borders.get(BorderType.RIGHT_BORDER).line_style = CellBorderType.DASHED | |
fc.style.borders.get(BorderType.TOP_BORDER).line_style = CellBorderType.DASHED | |
fc.style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.DASHED | |
fc.style.borders.get(BorderType.LEFT_BORDER).color = Color.from_argb(0, 255, 255) | |
fc.style.borders.get(BorderType.RIGHT_BORDER).color = Color.from_argb(0, 255, 255) | |
fc.style.borders.get(BorderType.TOP_BORDER).color = Color.from_argb(0, 255, 255) | |
fc.style.borders.get(BorderType.BOTTOM_BORDER).color = Color.from_argb(255, 255, 0) | |
workbook.save(dataDir + "output.xlsx") |
تعيين النمط
from aspose.cells import BackgroundType, CellArea, FormatConditionType, OperatorType, Workbook | |
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(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
sheet = workbook.worksheets[0] | |
# Adds an empty conditional formatting | |
index = sheet.conditional_formattings.add() | |
fcs = sheet.conditional_formattings[index] | |
# Sets the conditional format range. | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 5 | |
ca.start_column = 0 | |
ca.end_column = 3 | |
fcs.add_area(ca) | |
# Adds condition. | |
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100") | |
fc = fcs[conditionIndex] | |
fc.style.pattern = BackgroundType.REVERSE_DIAGONAL_STRIPE | |
fc.style.foreground_color = Color.from_argb(255, 255, 0) | |
fc.style.background_color = Color.from_argb(0, 255, 255) | |
workbook.save(dataDir + "output.xlsx") |
مواضيع متقدمة
- إضافة التدرج اللوني ثنائي اللون والثلاثي اللون
- تطبيق التنسيق الشرطي في الأوراق العمل
- تطبيق التظليل على الصفوف والأعمدة البديلة باستخدام التنسيق الشرطي
- توليد صور شريط بيانات التنسيق الشرطي
- الحصول على مجموعات الرموز، أشرطة البيانات أو كائنات مقياس الألوان المستخدمة في التنسيق الشرطي