إعدادات المحاذاة

ضبط إعدادات المحاذاة

إعدادات المحاذاة في Microsoft Excel

أي شخص قد استخدم Microsoft Excel لتنسيق الخلايا سيكون متعودًا على إعدادات المحاذاة في Microsoft Excel.

كما يمكنك رؤية من الشكل أعلاه، هناك أنواع مختلفة من خيارات المحاذاة:

  • محاذاة النص (أفقية وعمودية)
  • المسافة البادئة.
  • التوجيه.
  • التحكم بالنص.
  • اتجاه النص.

جميع هذه الإعدادات للمحاذاة مدعومة بالكامل من قبل Aspose.Cells لـ Python via .NET وتناقش بمزيد من التفصيل أدناه.

إعدادات المحاذاة في Aspose.Cells لـ Python via .NET

يقدم Aspose.Cells لـ Python via .NET فئة، Workbook، التي تمثل ملف إكسل. تحتوي فئة Workbook على مجموعة worksheets تسمح بالوصول إلى كل ورقة عمل في ملف الإكسل. تمثل ورقة العمل بواسطة فئة Worksheet. توفر فئة Worksheet مجموعة cells. كل عنصر في مجموعة cells يمثل كائنًا من فئة Cell.

يوفر Aspose.Cells لـ Python via .NET طرق get_style و set_style للفئة Cell التي تُستخدم للحصول على وتعيين تنسيق الخلية. تقدم فئة Style خصائص مفيدة لضبط إعدادات المحاذاة.

حدد أي نوع لمحاذاة النص باستخدام تعداد TextAlignmentType. أنواع محاذاة النص المحددة مسبقًا في تعداد TextAlignmentType هي:

** أنواع محاذاة النص ** ** الوصف **
GENERAL يمثل محاذاة النص العامة
BOTTOM يمثل محاذاة النص من الأسفل
CENTER يمثل محاذاة النص في الوسط
CENTER_ACROSS يمثل محاذاة النص عبر الوسط
DISTRIBUTED يمثل محاذاة النص الموزع
FILL يمثل ملء محاذاة النص
JUSTIFY يمثل تبرير محاذاة النص
LEFT يمثل محاذاة النص ناحية اليسار
RIGHT يمثل محاذاة النص ناحية اليمين
TOP يمثل محاذاة النص العلوية
JUSTIFIED_LOW يتم محاذاة النص مع ضبط طول الكاشيدة للنص العربي
THAI_DISTRIBUTED توزيع النص التايلاندي بشكل خاص، لأن كل حرف يُعامل ككلمة

المحاذاة الأفقية

استخدم خاصية horizontal_alignment في Style لمحاذاة النص أفقياً.

from aspose.cells import SaveFormat, TextAlignmentType, 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()
# Obtaining the reference of the worksheet
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Visit Aspose!")
# Setting the horizontal alignment of the text in the "A1" cell
style = cell.get_style()
style.horizontal_alignment = TextAlignmentType.CENTER
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003)

المحاذاة الرأسية

مشابهة للمحاذاة الأفقية، استخدم خاصية vertical_alignment في Style لمحاذاة النص عمودياً.

from aspose.cells import SaveFormat, TextAlignmentType, 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()
# Clearing all the worksheets
workbook.worksheets.clear()
# 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("Visit Aspose!")
# Setting the horizontal alignment of the text in the "A1" cell
style = cell.get_style()
# Setting the vertical alignment of the text in a cell
style.vertical_alignment = TextAlignmentType.CENTER
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003)

المسافة البادئة

من الممكن تعيين مستوى المسافة البادئة للنص في خلية بواسطة خاصية indent_level في Style.

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()
# Obtaining the reference of the worksheet
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Visit Aspose!")
# Setting the horizontal alignment of the text in the "A1" cell
style = cell.get_style()
# Setting the indentation level of the text (inside the cell) to 2
style.indent_level = 2
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003)

الاتجاه

حدد اتجاه (دوران) النص في خلية بواسطة خاصية rotation_angle في Style.

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()
# Obtaining the reference of the worksheet
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Visit Aspose!")
# Setting the horizontal alignment of the text in the "A1" cell
style = cell.get_style()
# Setting the rotation of the text (inside the cell) to 25
style.rotation_angle = 25
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003)

التحكم في النص

يناقش القسم التالي كيفية التحكم في النص عن طريق تعيين التفاف النص، وتقليل حجم النص للتناسب وخيارات التنسيق الأخرى.

تفاف النص

يعمل تفاف النص في خلية على جعل النص أسهل قراءة: يتم ضبط ارتفاع الخلية ليتناسب مع جميع النص، بدلاً من قطعه أو تسربه إلى الخلايا المجاورة. ضبط التفاف النص على تشغيل أو إيقاف بواسطة خاصية is_text_wrapped في Style.

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(".")
# Create Workbook Object
wb = Workbook()
# Open first Worksheet in the workbook
ws = wb.worksheets[0]
# Get Worksheet Cells Collection
cell = ws.cells
# Increase the width of First Column Width
cell.set_column_width(0, 35)
# Increase the height of first row
cell.set_row_height(0, 36)
# Add Text to the Firts Cell
cell.get(0, 0).put_value("I am using the latest version of Aspose.Cells to test this functionality")
# Make Cell's Text wrap
style = cell.get(0, 0).get_style()
style.is_text_wrapped = True
cell.get(0, 0).set_style(style)
# Save Excel File
wb.save(dataDir + "WrappingText.out.xlsx")
تقليص للتناسب

خيار لتفاف النص في حقل هو تصغير حجم النص ليتناسب مع أبعاد الخلية. يتم ذلك بضبط خاصية is_text_wrapped في Style إلى true.

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()
# Obtaining the reference of the worksheet
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Visit Aspose!")
# Setting the horizontal alignment of the text in the "A1" cell
style = cell.get_style()
# Shrinking the text to fit according to the dimensions of the cell
style.shrink_to_fit = True
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003)
دمج الخلايا

مثل Microsoft Excel، يدعم Aspose.Cells for Python via .NET دمج عدة خلايا في خلية واحدة. يوفر Aspose.Cells for Python via .NET طريقتين لهذه المهمة. إحداهما هي استدعاء طريقة merge لمجموعة cells. تأخذ طريقة merge المعلمات التالية لدمج الخلايا:

  • الصف الأول: الصف الأول من حيث بدء الدمج.
  • العمود الأول: العمود الأول من حيث بدء الدمج.
  • عدد الصفوف: عدد الصفوف التي تم دمجها.
  • عدد الأعمدة: عدد الأعمدة المدمجة.
from aspose.cells import BackgroundType, 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)
# Create a Workbook.
wbk = Workbook()
# Create a Worksheet and get the first sheet.
worksheet = wbk.worksheets[0]
# Create a Cells object ot fetch all the cells.
cells = worksheet.cells
# Merge some Cells (C6:E7) into a single C6 Cell.
cells.merge(5, 2, 2, 3)
# Input data into C6 Cell.
worksheet.cells.get(5, 2).put_value("This is my value")
# Create a Style object to fetch the Style of C6 Cell.
style = worksheet.cells.get(5, 2).get_style()
# Create a Font object
font = style.font
# Set the name.
font.name = "Times New Roman"
# Set the font size.
font.size = 18
# Set the font color
font.color = Color.blue
# Bold the text
font.is_bold = True
# Make it italic
font.is_italic = True
# Set the backgrond color of C6 Cell to Red
style.foreground_color = Color.red
style.pattern = BackgroundType.SOLID
# Apply the Style to C6 Cell.
cells.get(5, 2).set_style(style)
# Save the Workbook.
wbk.save(dataDir + "mergingcells.out.xls")

الطريقة الأخرى هي أولاً استدعاء cells لجمع الخلايا المدمجة. الطريقة create_range في create_range تأخذ نفس مجموعة المعلمات كما في الطريقة merge المناقشة أعلاه وتعيد Range. الكائن Range يوفر أيضاً الطريقة merge التي تدمج المجموعة المحددة في الكائن Range.

اتجاه النص

من الممكن تعيين ترتيب قراءة النص في الخلايا. ترتيب القراءة هو الترتيب البصري الذي يظهر فيه الأحرف والكلمات وما إلى ذلك. على سبيل المثال، الإنجليزية هي لغة من اليسار إلى اليمين بينما العربية هي لغة من اليمين إلى اليسار.

يتم تعيين ترتيب القراءة بواسطة خاصية text_direction في كائن Style. يوفر Aspose.Cells for Python via .NET أنواعًا محددة مسبقًا لاتجاه النص في التعداد TextDirectionType.

أنواع توجيه النص الوصف
CONTEXT ترتيب القراءة متسق مع لغة الحرف المدخل الأول
LEFT_TO_RIGHT ترتيب القراءة من اليسار إلى اليمين
RIGHT_TO_LEFT ترتيب القراءة من اليمين إلى اليسار
from aspose.cells import TextDirectionType, Workbook
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of first worksheet
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("I am using the latest version of Aspose.Cells to test this functionality.")
# Gets style in the "A1" cell
style = cell.get_style()
# Shrinking the text to fit according to the dimensions of the cell
style.text_direction = TextDirectionType.LEFT_TO_RIGHT
cell.set_style(style)
# Saving the Excel file
workbook.save("book1.xlsx")

مواضيع متقدمة