Python for Finance: Analyze Big Financial Data

(Elle) #1
clear

Resets all properties/attributes of the cell

copy_from

Copies all properties/attributes from another cell

copy_format_from

Copies all properties/attributes from another cell except value and formula

is_empty

Returns True if empty

offset

Returns cell object given relative offset as (row, col) tuple

subtraction

Subtraction gives the offset; e.g., Cell(“B4”) - Cell(“A2”) gives (2, 1)

print

Gives name and sheet of cell

set_name

Sets named range in Excel; e.g., Cell(“A1”).set_name(“upper_left”)

Often, it is helpful to work with CellRange instead of Cell objects only. One can think of


this as an approach to vectorize certain operations on multiple Cell objects. Consider the


following examples, still based on the same spreadsheet file workbook.xlsx with our


previous changes:


In  [ 6 ]:  CellRange(“A1:A8”).value
Out[ 6 ]: [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]

In  [ 7 ]:  CellRange(“A1:A8”).value    =    1
# like broadcasting

In  [ 8 ]:  CellRange(“A1:A8”).value
Out[ 8 ]: [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ]

In  [ 9 ]:  CellRange(“A1:A8”).value    =    2  *   [ 1 ,    2 ,     3 ,     4 ]

In  [ 10 ]: CellRange(“A1:A8”).value
Out[ 10 ]: [ 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ]

In  [ 11 ]: Cell(“A9”).value
Out[ 11 ]: 20
# value of Sum function is
# automatically updated

Of course, you can also use CellRange for iteration:


In  [ 12 ]: for cell in CellRange(“A1:B2”):
....: print cell.name, cell.value
....:
A1 1
B1 Python with Excel
A2 2
B2 10

The majority of the Cell attributes and methods can also be used with CellRange.


When writing complex Python scripts for interaction with Excel spreadsheets,

Free download pdf