Justeringsinställningar

Konfigurera justeringsinställningar

Justeringsinställningar i Microsoft Excel

Alla som har använt Microsoft Excel för att formatera celler kommer att vara bekanta med justeringsinställningarna i Microsoft Excel.

Som du kan se från figuren ovan, finns det olika typer av justeringsalternativ:

  • Textjustering (horisontell & vertikal)
  • Indrag.
  • Orientering.
  • Textkontroll.
  • Textriktning.

Alla dessa justeringsinställningar stöds fullt ut av Aspose.Cells för Python via .NET och diskuteras i mer detalj nedan.

Justeringar i Aspose.Cells för Python via .NET

Aspose.Cells för Python via .NET tillhandahåller en klass, Workbook, som representerar en Excel-fil. Klassen Workbook innehåller en worksheets samling som ger tillgång till varje kalkylblad i filen. Ett kalkylblad representeras av klassen Worksheet. Klassen Worksheet ger en cells samling. Varje objekt i cells samlingen representerar ett objekt av klassen Cell.

Aspose.Cells för Python via .NET tillhandahåller get_style och set_style metoder för klassen Cell som används för att få och sätta cellformattering. Klassen Style ger användbara egenskaper för att konfigurera justeringsinställningar.

Välj vilken som helst textjusteringstyp med hjälp av uppräkningen TextAlignmentType. De fördefinierade textjusteringstyperna i uppräkningen TextAlignmentType är:

Textjusteringstyper Beskrivning
GENERAL Representerar generell textjustering
BOTTOM Representerar botten-justering
CENTER Representerar mittenjustering
CENTER_ACROSS Representerar centrum över cellraden justering
DISTRIBUTED Representerar distribuerad textjustering
FILL Representerar fyllnadsjustering
JUSTIFY Representerar justera textjustering
LEFT Representerar vänsterjustering
RIGHT Representerar högerjustering
TOP Representerar toppjustering
JUSTIFIED_LOW Justerar texten med anpassad kashida-längd för arabiska
THAI_DISTRIBUTED Distribuerar thaitext särskilt, eftersom varje tecken behandlas som ett ord

Horisontell justering

Använd Style objektets horizontal_alignment egenskap för att justera texten horisontellt.

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)

Vertikal justering

Liknande horisontell justering, använd Style objektets vertical_alignment egenskap för att justera texten vertikalt.

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)

Indrag

Det är möjligt att ange indelningsnivån för texten i en cell med Style objektets indent_level egenskap.

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)

Orientering

Ange orienteringen (rotationen) för texten i en cell med Style objektets rotation_angle egenskap.

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)

Textkontroll

I följande avsnitt diskuteras hur man kontrollerar text genom att ställa in textbrytning, krympa till passa och andra formateringsalternativ.

Textindrag

Att rada text i en cell gör det lättare att läsa: cellens höjd justeras för att passa all text istället för att klippa av den eller få den att rinna över i intilliggande celler. Ange textombrytning på eller av med Style objektets is_text_wrapped egenskap.

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")
Krympa passande

Ett alternativ för att rada text i ett fält är att krympa textstorleken för att passa en cells dimensioner. Detta görs genom att ställa in Style objektets is_text_wrapped egenskap till 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)
Sammanfoga celler

Precis som Microsoft Excel stöder Aspose.Cells för Python via .NET sammanslagning av flera celler till en. Aspose.Cells för Python via .NET erbjuder två tillvägagångssätt för detta. Ett sätt är att kalla cells samlingen merge metod. merge metoden tar följande parametrar för att slå samman cellerna:

  • Första rad: den första raden från vilken sammanfogningen ska börja.
  • Första kolumn: den första kolumnen från vilken sammanfogningen ska börja.
  • Antal rader: antalet rader att sammanfoga.
  • Antal kolumner: antalet kolumner att sammanfoga.
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")

Det andra sättet är att först anropa cells samlingen create_range metod för att skapa en räckvidd av celler som ska sammanfogas. create_range metoden tar samma parametrar som merge metoden som diskuterades ovan och returnerar ett Range objekt. Range objektet tillhandahåller också en merge metod som sammanfogar den angivna räckvidden i Range objektet.

Textriktning

Det är möjligt att ställa in läsordningen för text i celler. Läsordningen är den visuella ordningen där tecken, ord osv. visas. Till exempel är engelska ett vänster-till-höger-språk medan arabiska är ett höger-till-vänster-språk.

Läsordningen sätts med Style objektets text_direction egenskap. Aspose.Cells för Python via .NET tillhandahåller fördefinierade textriktningstyper i TextDirectionType uppräkningen.

Textriktningstyper Beskrivning
CONTEXT Läsordningen är konsekvent med språket för den första tecknet som matas in
LEFT_TO_RIGHT Läsordning från vänster till höger
RIGHT_TO_LEFT Läsordning från höger till vänster
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")

Fortsatta ämnen