API

xl_types.py

class xl_link.xl_types.XLRange(start, stop)

Represents a range in an Excel spreadsheet

Parameters:
  • start (XLCell) – corresponding to start cell
  • stop (XLCell) – corresponding to stop cell
start

XLCell – corresponding to start cell

stop

XLCell – corresponding to stop cell

sheet

str – name of sheet range exists in

Examples

Using __init__

>>> start = XLCell.from_cell('A1')
>>> stop = XLCell.from_cell('B7')
>>> XLRange(start, stop)
    <XLRange: A1:B7>

Notes

Upon initialisation, if start and stop are not from the same sheet, an assertion error is raised.

start and stop are attributes that can be accessed and modified directly.

All methods are designed to return new objects, preserving the state of self.

frange

Gets the Excel range this object represents, for use in excel formulas

Returns:representing this range in excel notation for use in excel formulas (e.g. “’{sheet}’!{start}:{stop}”
Return type:str

Examples

>>> start = XLCell.from_cell('A1')
>>> stop = XLCell.from_cell('B7')
>>> range = XLRange(start, stop)
>>> range.frange
    "'Sheet1'!A1:B7"
classmethod from_frange(frange)

Alternative constructor

Parameters:frange (str) – str in excel formula notation (e.g. “‘Sheet1’!A1:B20”), representing excel range.
Returns:initialised XLRange
Return type:XLRange
classmethod from_range(range, sheet='Sheet1')

Alternative constructor

Parameters:
  • range (str) – str in excel notation (e.g. ‘A1:B20’), representing excel range.
  • sheet (str) – sheet range belongs to, default=’Sheet1’
Returns:

initialised XLRange

Return type:

XLRange

iterrows()

Iterate over each row of self, yields each row as XLRange

Yields:XLRange – corresponding to current row
range

Gets the Excel range this object represents

Returns:representing this range in excel notation
Return type:str

Examples

>>> start = XLCell.from_cell('A1')
>>> stop = XLCell.from_cell('B7')
>>> range = XLRange(start, stop)
>>> range.range
    'A1:B7'
rowcol_rowcol

Coordinates of start and stop

Returns:
  • start.row, stop.col (tuple) – start position in rows and column num (start.row, start.col)
  • stop.row, stop.col (tuple) – stop position in rows and column num (stop.row, stop.col)
shape

returns: representing shape of XLRange in form (height, width) :rtype: tuple

trans(row, col)

Short for XLRange.translate

translate(row, col)

Translates whole range by row, col.

Parameters:
  • row (int) – corresponding to movement in row direction (+ve down the spreadsheet)
  • col (int) – corresponding to movement in col direction (+ve right across the spreadsheet)
Returns:

new with translation applied

Return type:

XLRange

Notes

This moves the whole range, and cannot be used to change the shape of self.

class xl_link.xl_types.XLCell(row, col, sheet='Sheet1')

Represents the location of a cell in an Excel spreadsheet

Parameters:
  • row (int) – corresponding to cell row number (0 indexed)
  • col (int) – corresponding to cell col number (0 indexed)
  • sheet (str) – corresponding to the sheet the cell is in, default=’Sheet1’
row

int – corresponding to cell row number (0 indexed)

col

int – corresponding to cell col number (0 indexed)

sheet

str – corresponding to the sheet the cell is in, default=’Sheet1’

Examples

>>> XLCell(0, 5, 'Accounts')
    <XLCell: F1>
>>> XLCell.from_cell('F1', 'Accounts')
    <XLCell: F1>
>>> XLCell.from_fcell("'Accounts'!F1")
    <XLCell: F1>

Notes

sheet can be set to None if desired, keep in mind it defaults to ‘Sheet1’, mostly for convenience.

row, col and sheet are attributes that can be accessed and set directly.

All methods are designed to return new objects, preserving the state of self.

cell

Gets the cell location str.

Returns:cell location in excel notation
Return type:str

Examples

>>> cell = XLCell(0, 5, 'Accounts')
>>> cell.cell
    'F1'
copy()
Returns:copy of self
Return type:XLCell
fcell

Gets the cell location for use in excel formulas.

Returns:cell location in excel notation, in ‘{sheet}’!{cell} form
Return type:str

Examples

>>> cell = XLCell(0, 5, 'Accounts')
>>> cell.fcell
    "'Accounts'!F1"
classmethod from_cell(cell, sheet='Sheet1')

Alternative constructor

Parameters:
  • cell (str) – cell in excel notation (e.g. ‘A1’), representing cell location.
  • sheet (sheet) – sheet cell belongs to, default=’Sheet1’
Returns:

Initialised XLCell

Return type:

XLCell

classmethod from_fcell(fcell)

Alternative constructor

Parameters:fcell (str) – cell in excel formula notation (e.g. “‘Sheet1’!A1”), representing cell location.
Returns:Initialised XLCell
Return type:XLCell
range_between(other)

Get XLRange between this cell and other.

Parameters:other (XLCell) – other XLCell that makes up stop of excel range
Returns:range between this cell and other
Return type:XLRange

Example

>>> start = XLCell.from_cell('A1')
>>> stop = XLCell.from_cell('B7')
>>> start.range_between(stop)
    <XLRange: A1:B7>
rowcol

Coordinates of cell

Returns:row, col – (row, col) corresponding to location of cell
Return type:tuple(int, int)
trans(row, col)

Short for XLCell.translate

translate(row, col)

Returns new XLCell with translation applied

Parameters:
  • row (int) – corresponding to movement in row direction (+ve down the spreadsheet)
  • col (int) – corresponding to movement in col direction (+ve right across the spreadsheet)
Returns:

new cell with translation applied

Return type:

XLCell

chart_wrapper.py

xl_link.chart_wrapper.create_chart(workbook, engine, type_, values, categories, names, subtype=None, title=None, x_axis_name=None, y_axis_name=None)

Create a chart object corresponding to given engine, within workbook.

Parameters:
  • workbook (object) – to insert chart into, either XlsxWriter.Workbooks or openpyxl Workbooks.
  • engine (str) – representing engine to use, either XlsxWriter.Workbooks or openpyxl Workbooks.
  • type (str) – representing chart type, see engine docs for options e.g. ‘line’.
  • values (XLRange or sequence of XLRanges) – use as values for each series/ data set (Excel equivalent to y data).
  • categories (XLRange or sequence of XLRanges) – use as categories for each series/ data set (Excel equivalent to x data).
  • names (str/ sequence of str) – use as name for each series/ data set (Excel uses this to label each dataset).
  • subtype (str) – representing chart subtype, see engine docs for details.
Returns:

chart – populated chart object corresponding to engine’s chart type.

Return type:

object

class xl_link.chart_wrapper.AbstractChartWrapper(book, type_, subtype=None, engine=None)

Wraps around excel writer module, for use by create_chart.

add_series(name, values, categories=None)

Abstract: should add a series to chart.

Parameters:
  • name (str) – representing the name of the series
  • values (XLRange) – represending values of series
  • categories (XLRange) – representing values of categories