Настройки шрифта

Настройка настроек шрифта

Aspose.Cells для Python via .NET предоставляет класс Workbook, который представляет файл Microsoft Excel. Класс Workbook содержит коллекцию worksheets, которая обеспечивает доступ к каждому листу в файле Excel. Лист представлен классом Worksheet. Класс Worksheet предоставляет коллекцию cells. Каждый элемент коллекции cells представляет объект класса Cell.

Aspose.Cells предоставляет методы класса Cell для получения и установки стиля форматирования ячейки. Класс get_style предоставляет свойства для настройки настроек шрифта.

Установка названия шрифта

Разработчики могут применять любой шрифт к тексту внутри ячейки, используя свойство name объекта 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)

Установка стиля шрифта на жирный

Разработчики могут сделать текст жирным, установив свойство is_bold объекта Style.font в 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")

Установка размера шрифта

Установите размер шрифта с помощью свойства size объекта 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")

Установка цвета шрифта

Используйте свойство color объекта Style.font для установки цвета шрифта. Выберите любой цвет из перечисления Color (часть .NET framework) и присвойте его свойству 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")

Установка типа подчеркивания шрифта

Используйте свойство underline объекта Style.font, чтобы подчеркнуть текст. Aspose.Cells для Python via .NET предлагает различные предопределённые типы подчеркивания шрифта в перечислении FontUnderlineType.

Типы подчеркивания шрифта Описание
БЮЛЛЕТЕНИЙ Одинарное подчеркивание бухгалтерии
ДВОЙНОЕ Двойное подчеркивание
ДВОЙНОЕ_БЮЛЛЕТЕНИЕ Двойное бухгалтерское подчеркивание
НИЧЕГО Без подчеркивания
ОДИНАРНОЕ Одно подчеркивание
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")

Установка эффекта зачеркивания

Примените зачеркивание, установив свойство is_strikeout объекта Style.font в 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")

Установка эффекта нижнего индекса

Примените нижний индекс, установив свойство is_subscript объекта Style.font в 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")

Установка верхнего индекса на шрифт

Разработчики могут применить эффект верхнего индекса к шрифту, установив свойство is_superscript объекта Style.font в 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")

Продвинутые темы