Impostazioni del carattere

Configurazione delle Impostazioni del Carattere

Aspose.Cells per Python via .NET fornisce una classe, Workbook che rappresenta un file Microsoft Excel. La classe Workbook contiene una raccolta worksheets che consente di accedere a ogni foglio di lavoro in un 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 fornisce i metodi get_style e set_style della classe Cell utilizzati per ottenere e impostare lo stile di formattazione di una cella. La classe Style fornisce proprietà per configurare le impostazioni del carattere.

Impostazione del nome del carattere

Gli sviluppatori possono applicare qualsiasi font al testo all’interno di una cella usando la proprietà Style.font dell’oggetto name.

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 name to "Times New Roman"
style.font.name = "Times New Roman"
# 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)

Impostare lo stile del carattere su Grassetto

Gli sviluppatori possono rendere il testo in grassetto impostando la proprietà is_bold dell’oggetto Style.font su true.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# 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("out.xlsx")

Impostazione della dimensione del carattere

Imposta la dimensione del carattere con la proprietà size dell’oggetto Style.font.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# 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 size to 14
style.font.size = 14
# Applying the style to the cell
cell.set_style(style)
# Saving the Excel file
workbook.save("out.xlsx")

Impostazione del colore del carattere

Utilizzare la proprietà color dell’oggetto Style.font per impostare il colore del carattere. Selezionare qualsiasi colore dall’enumerazione Color (parte del framework .NET) e assegnarlo alla proprietà color.

from aspose.cells import Workbook
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# 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 color to blue
style.font.color = Color.blue
# Applying the style to the cell
cell.set_style(style)
# Saving the Excel file
workbook.save("out.xlsx")

Impostazione del tipo sottolineato del carattere

Usa la proprietà underline dell’oggetto Style.font per sottolineare il testo. Aspose.Cells per Python via .NET offre vari tipi di sottolineatura predefiniti nella enumerazione FontUnderlineType.

Tipi di sottolineatura del carattere Descrizione
ACCOUNTING Sottolineatura contabile singola
DOUBLE Doppia sottolineatura
DOUBLE_ACCOUNTING Sottolineatura contabile doppia
NONE Nessuna sottolineatura
SINGLE Sottolineatura singola
from aspose.cells import FontUnderlineType, 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 to be underlined
style.font.underline = FontUnderlineType.SINGLE
# Applying the style to the cell
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "out.xlsx")

Impostazione dell’effetto barrato

Applicare il barrato impostando la proprietà is_strikeout dell’oggetto Style.font su true.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# 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 strike out effect on the font
style.font.is_strikeout = True
# Applying the style to the cell
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "out.xlsx")

Impostazione dell’effetto pedice

Applica il pedice impostando la proprietà is_subscript dell’oggetto Style.font su true.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# 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 subscript effect
style.font.is_subscript = True
# Applying the style to the cell
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "out.xlsx")

Impostazione dell’effetto esponente sulla font

I programmatori possono applicare l’effetto esponente sulla font impostando la proprietà is_superscript dell’oggetto Style.font su true.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# 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 superscript effect
style.font.is_superscript = True
# Applying the style to the cell
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "out.xlsx")

Argomenti avanzati