Configuración de fuente

Configuración de fuente

Aspose.Cells para Python via .NET proporciona una clase, Workbook que representa un archivo de Microsoft Excel. La clase Workbook contiene una colección worksheets que permite acceder a cada hoja de cálculo en un archivo de Excel. Una hoja de cálculo está representada por la clase Worksheet. La clase Worksheet proporciona una colección cells. Cada elemento en la colección cells representa un objeto de la clase Cell.

Aspose.Cells proporciona las clases Cell y los métodos get_style y set_style que se utilizan para obtener y establecer el estilo de formato de una celda. La clase Style proporciona propiedades para configurar la configuración de fuente.

Establecer nombre de fuente

Los desarrolladores pueden aplicar cualquier fuente al texto dentro de una celda mediante la propiedad name del objeto Style.font.

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)

Establecer estilo de fuente en negrita

Los desarrolladores pueden hacer que el texto esté en negrita configurando la propiedad is_bold del objeto Style.font en 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")

Establecer tamaño de fuente

Establezca el tamaño de fuente con la propiedad size del objeto 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")

Establecer color de fuente

Utilice la propiedad color del objeto Style.font para establecer el color de fuente. Seleccione cualquier color de la enumeración Color (parte del marco .NET) y asígnele a la propiedad 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")

Establecer tipo de subrayado de fuente

Use la propiedad underline del objeto Style.font para subrayar el texto. Aspose.Cells para Python via .NET ofrece varios tipos predefinidos de subrayado en la enumeración FontUnderlineType.

Tipos de subrayado de fuente Descripción
ACCOUNTING Un solo subrayado de contabilidad
DOUBLE Subrayado doble
DOUBLE_ACCOUNTING Subrayado de contabilidad doble
NONE Sin subrayado
SINGLE Un solo subrayado
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")

Establecer efecto tachado

Aplicar el tachado configurando la propiedad is_strikeout del objeto Style.font en 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")

Establecer efecto subíndice

Aplicar subíndice configurando la propiedad is_subscript del objeto Style.font en 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")

Establecer efecto superíndice en fuente

Los desarrolladores pueden aplicar el efecto superíndice en la fuente configurando la propiedad is_superscript del objeto Style.font en 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")

Temas avanzados