Impostazioni di Allineamento
Configurazione delle impostazioni di allineamento
Impostazioni di allineamento in Microsoft Excel
Chiunque abbia usato Microsoft Excel per formattare le celle sarà familiare con le impostazioni di allineamento in Microsoft Excel.
Come si può vedere dalla figura sopra, ci sono diversi tipi di opzioni di allineamento:
- Allineamento del testo (orizzontale e verticale)
- Rientro.
- Orientamento.
- Controllo del testo.
- Direzione del testo.
Tutte queste impostazioni di allineamento sono pienamente supportate da Aspose.Cells per Python via .NET e sono discusse più in dettaglio sotto.
Impostazioni di allineamento in Aspose.Cells per Python via .NET
Aspose.Cells per Python via .NET fornisce una classe, Workbook, che rappresenta un file Excel. La classe Workbook contiene una raccolta worksheets che consente l’accesso a ogni foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta cells. Ogni elemento nella raccolta cells rappresenta un oggetto della classe Cell.
Aspose.Cells per Python via .NET fornisce i metodi get_style e set_style per la classe Cell che vengono usati per ottenere e impostare la formattazione di una cella. La classe Style fornisce proprietà utili per configurare le impostazioni di allineamento.
Seleziona qualsiasi tipo di allineamento del testo utilizzando l’enumerazione TextAlignmentType. I tipi di allineamento del testo predefiniti nell’enumerazione TextAlignmentType sono:
Tipi di Allineamento del Testo | Descrizione |
---|---|
GENERAL | Rappresenta l’allineamento generale del testo |
BOTTOM | Rappresenta l’allineamento del testo in basso |
CENTER | Rappresenta l’allineamento del testo al centro |
CENTER_ACROSS | Rappresenta l’allineamento del testo centrato attraverso |
DISTRIBUTED | Rappresenta l’allineamento distribuito del testo |
FILL | Rappresenta l’allineamento del testo di riempimento |
JUSTIFY | Rappresenta l’allineamento del testo giustificato |
LEFT | Rappresenta l’allineamento del testo a sinistra |
RIGHT | Rappresenta l’allineamento del testo a destra |
TOP | Rappresenta l’allineamento del testo in alto |
JUSTIFIED_LOW | Allinea il testo con una lunghezza di kashida regolata per il testo arabo |
THAI_DISTRIBUTED | Distribuisce il testo tailandese in modo speciale, poiché ogni carattere è trattato come una parola |
Allineamento Orizzontale
Utilizzare la proprietà horizontal_alignment dell’oggetto Style per allineare il testo orizzontalmente.
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) |
Allineamento Verticale
Similmente all’allineamento orizzontale, utilizzare la proprietà vertical_alignment dell’oggetto Style per allineare il testo verticalmente.
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) |
Rientro
È possibile impostare il livello di rientro del testo in una cella con la proprietà indent_level dell’oggetto 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) |
Orientamento
Imposta l’orientamento (rotazione) del testo in una cella con la proprietà rotation_angle dell’oggetto 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) |
Controllo del Testo
La seguente sezione discute come controllare il testo impostando il rientro del testo, adattamento alla cella e altre opzioni di formattazione.
Testo a Capo
Il testo a capo in una cella rende più facile leggerlo: l’altezza della cella si adatta per contenere tutto il testo, invece di tagliarlo o farlo fuoriuscire nelle celle adiacenti. Imposta il testo a capo on o off con la proprietà is_text_wrapped dell’oggetto 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") |
Ridimensionamento per adattarsi
Un’opzione per avvolgere il testo in un campo è ridurre le dimensioni del testo per adattarlo alle dimensioni di una cella. Questo viene fatto impostando la proprietà is_text_wrapped dell’oggetto Style su 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) |
Unione di celle
Come Microsoft Excel, Aspose.Cells per Python via .NET supporta la fusione di più celle in una. Aspose.Cells per Python via .NET fornisce due approcci per questo compito. Un modo è chiamare il metodo merge della raccolta cells. Il metodo merge prende i seguenti parametri per unire le celle:
- Prima riga: la prima riga da cui iniziare a unire.
- Prima colonna: la prima colonna da cui iniziare a unire.
- Numero di righe: il numero di righe da unire.
- Numero di colonne: il numero di colonne da unire.
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") |
L’altro modo è chiamare prima il metodo create_range della raccolta cells per creare un intervallo di celle da unire. Il metodo create_range richiede lo stesso set di parametri di quello del metodo merge discusso sopra e restituisce un oggetto Range. L’oggetto Range fornisce anche un metodo merge che unisce l’intervallo specificato nell’oggetto Range.
Direzione del testo
È possibile impostare l’ordine di lettura del testo nelle celle. L’ordine di lettura è l’ordine visivo in cui vengono visualizzati i caratteri, le parole, ecc. Ad esempio, l’inglese è una lingua da sinistra a destra mentre l’arabo è una lingua da destra a sinistra.
L’ordine di lettura è impostato con la proprietà text_direction dell’oggetto Style. Aspose.Cells per Python via .NET fornisce tipi di direzione del testo predefiniti nella enumerazione TextDirectionType.
Tipi di direzione del testo | Descrizione |
---|---|
CONTEXT | L’ordine di lettura coerente con la lingua del primo carattere inserito |
LEFT_TO_RIGHT | Ordine di lettura da sinistra a destra |
RIGHT_TO_LEFT | Ordine di lettura da destra a sinistra |
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") |