Cell: value, formula, and style

Overview

The Cell class represents a single cell in a worksheet and provides access to its core attributes. It exposes read/write properties for value, formula, and style, along with helper methods put_value(), get_style(), and apply_style().

NameKindDescription
valuepropertyGets or sets the cell’s value
formulapropertyGets or sets the cell’s formula string
stylepropertyGets the cell’s Style object (read-only)
commentpropertyGets the cell’s comment (read-only)
data_typepropertyGets the cell’s data type (read-only)
put_value(val)methodSets the cell’s value from a Python scalar
get_style()methodReturns a mutable copy of the cell’s style
apply_style(style)methodApplies a modified Style object back to the cell
from aspose.cells_foss import Workbook

workbook = Workbook()
worksheet = workbook.worksheets[0]
worksheet.cells["A1"].value = "Hello"
worksheet.cells["B1"].formula = "=A1"

Constructor

The Cell class constructor is not exposed publicly. Cells are obtained through the Cells collection of a worksheet using A1 notation or zero-based row/column indices.

ParameterTypeDescription
(none)N/AConstructor is not public; cells are accessed via worksheet.cells["A1"] or worksheet.cells[row, col]
from aspose.cells_foss import Workbook

workbook = Workbook()
worksheet = workbook.worksheets[0]
cell = worksheet.cells["A1"]

Properties

The Cell class exposes the following properties. value and formula are read/write; style, comment, and data_type are read-only (use get_style() / apply_style() to modify style).

NameTypeRead/WriteDescription
valueobjectread/writeThe cell’s current value (string, int, float, bool, datetime, or None)
formulastrread/writeThe cell’s formula string, including the leading =
styleStyleread-onlyThe cell’s style object; modify via get_style() / apply_style()
commentstrread-onlyThe cell’s comment text
data_typestrread-onlyThe cell’s data type identifier

Methods

MethodReturn TypeDescription
put_value(val)NoneSets the cell’s value to the given scalar
get_style()StyleReturns a mutable copy of the cell’s style for editing
apply_style(style)NoneApplies a modified Style object to the cell

Example

The following example demonstrates reading and writing cell properties using the Cell class. Note that value and formula are properties accessed with assignment syntax, not method calls.

from aspose.cells_foss import Workbook

# Create a new workbook and access the first worksheet
workbook = Workbook()
worksheet = workbook.worksheets[0]

# Set a cell value using property assignment
cell_a1 = worksheet.cells["A1"]
cell_a1.value = "Hello World"

# Set a formula using property assignment
cell_b1 = worksheet.cells["B1"]
cell_b1.formula = "=LEN(A1)"

# Read cell properties using the property (no parentheses)
value = cell_a1.value
formula = cell_b1.formula

print(f"Value: {value}")
print(f"Formula: {formula}")

# Modify style via get_style / apply_style
style = cell_a1.get_style()
style.font.bold = True
style.font.size = 14
cell_a1.apply_style(style)

workbook.save("output.xlsx")

See Also