Ausrichtungseinstellungen
Konfigurieren von Ausrichtungseinstellungen
Ausrichtungseinstellungen in Microsoft Excel
Jeder, der Microsoft Excel verwendet hat, um Zellen zu formatieren, wird mit den Ausrichtungseinstellungen in Microsoft Excel vertraut sein.
Wie Sie aus der obigen Abbildung sehen können, gibt es verschiedene Arten von Ausrichtungsoptionen:
- Textausrichtung (horizontal & vertikal)
- Einrückung
- Orientierung
- Textkontrolle
- Textrichtung
Alle diese Ausrichtungseinstellungen werden vollständig von Aspose.Cells für Python via .NET unterstützt und im Folgenden ausführlich erläutert.
Ausrichtungseinstellungen in Aspose.Cells für Python via .NET
Aspose.Cells für Python via .NET bietet eine Klasse, Workbook, die eine Excel-Datei repräsentiert. Die Klasse Workbook enthält eine worksheets Sammlung, die Zugriff auf jedes Arbeitsblatt in der Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch die Klasse Worksheet dargestellt. Die Klasse Worksheet bietet eine cells Sammlung. Jedes Element in der Sammlung cells stellt ein Objekt der Klasse Cell dar.
Aspose.Cells für Python via .NET stellt get_style und set_style Methoden für die Cell Klasse bereit, mit denen das Format einer Zelle abgerufen und festgelegt werden kann. Die Style Klasse bietet nützliche Eigenschaften für die Konfiguration von Ausrichtungseinstellungen.
Wählen Sie einen beliebigen Textausrichtungstyp mithilfe der Aufzählung TextAlignmentType aus. Die vordefinierten Textausrichtungstypen in der Aufzählung TextAlignmentType lauten:
Textausrichtungstypen | Beschreibung |
---|---|
ALLGEMEIN | Repräsentiert allgemeine Textausrichtung |
UNTEN | Repräsentiert untere Textausrichtung |
MITTE | Repräsentiert mittlere Textausrichtung |
MITTE_QUER | Repräsentiert mittige Textausrichtung über die Zelle hinweg |
VERTEILT | Repräsentiert verteilte Textausrichtung |
FÜLLEN | Repräsentiert Fülltextausrichtung |
JUSTIEREN | Repräsentiert blockierende Textausrichtung |
LINKS | Repräsentiert linke Textausrichtung |
RECHTS | Repräsentiert rechte Textausrichtung |
OBEN | Repräsentiert obere Textausrichtung |
NEUJUSTIERT_LOW | Richtet den Text mit einer angepassten Kashida-Länge für Arabisch aus |
THAI_VERTEILT | Verteilt thailändischen Text speziell, da jedes Zeichen als Wort behandelt wird |
Horizontale Ausrichtung
Verwenden Sie die Eigenschaft horizontal_alignment des Objekts Style, um den Text horizontal auszurichten.
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) |
Vertikale Ausrichtung
Ähnlich wie bei der horizontalen Ausrichtung verwenden Sie die Eigenschaft vertical_alignment des Objekts Style, um den Text vertikal auszurichten.
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) |
Einrückung
Es ist möglich, den Einrückungsgrad des Textes in einer Zelle mit der Style -Objekteigenschaft indent_level festzulegen.
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) |
Ausrichtung
Legen Sie die Ausrichtung (Rotation) des Textes in einer Zelle mit der Style -Objekteigenschaft rotation_angle fest.
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) |
Textsteuerung
Im Folgenden wird erläutert, wie Sie Text steuern können, indem Sie Textrahmen, Anpassung an die Größe und andere Formatierungsoptionen festlegen.
Textumschlag
Das Umwickeln von Text in einer Zelle erleichtert das Lesen: Die Höhe der Zelle passt sich an, um den gesamten Text aufzunehmen, anstatt ihn abzuschneiden oder über benachbarte Zellen überlaufen zu lassen. Legen Sie das Textumwickeln mit der Style-Objekteigenschaft is_text_wrapped ein oder aus.
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") |
Anpassen an Größe
Eine Option zum Umwickeln von Text in einem Feld ist das Verkleinern der Textgröße, um sich an die Abmessungen einer Zelle anzupassen. Dies erfolgt durch Festlegen der Style-Objekteigenschaft is_text_wrapped auf 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) |
Zellen zusammenführen
Wie Microsoft Excel unterstützt Aspose.Cells für Python via .NET das Zusammenführen mehrerer Zellen zu einer. Aspose.Cells für Python via .NET bietet zwei Ansätze für diese Aufgabe. Ein Weg ist, die Methode merge der cells Sammlung aufzurufen. Die merge Methode benötigt die folgenden Parameter zum Zusammenführen der Zellen:
- Erste Zeile: Die erste Zeile, ab der das Zusammenführen beginnt.
- Erste Spalte: Die erste Spalte, ab der das Zusammenführen beginnt.
- Anzahl der Zeilen: Die Anzahl der zu zusammenführenden Zeilen.
- Anzahl der Spalten: Die Anzahl der zu zusammenführenden Spalten.
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") |
Der andere Weg besteht darin, zuerst die cells-Sammlungsmethode create_range aufzurufen, um einen Bereich der zu zusammenführenden Zellen zu erstellen. Die create_range-Methode akzeptiert denselben Satz von Parametern wie die zuvor diskutierte merge-Methode und gibt ein Range-Objekt zurück. Das Range-Objekt bietet auch eine merge-Methode, die den im Range-Objekt angegebenen Bereich zusammenführt.
Textausrichtung
Es ist möglich, die Lesereihenfolge von Text in Zellen festzulegen. Die Lesereihenfolge gibt die visuelle Reihenfolge an, in der Zeichen, Wörter usw. angezeigt werden. Zum Beispiel ist Englisch eine von links nach rechts lesbare Sprache, während Arabisch eine von rechts nach links lesbare Sprache ist.
Die Lesereihenfolge wird mit der Eigenschaft text_direction des Style Objekts festgelegt. Aspose.Cells für Python via .NET bietet vordefinierte Textausrichtungstypen in der TextDirectionType Enumeration.
Text Direction Types | Beschreibung |
---|---|
KONTEXT | Die Lesereihenfolge entspricht der Sprache des ersten eingegebenen Zeichens |
LINKS_NACH_RECHTS | Lesereihenfolge von links nach rechts |
RECHTS_NACH_LINKS | Lesereihenfolge von rechts nach links |
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") |