Workbook Object

In order to use xlwings, creating a workbook object is always the first thing to do:

class xlwings.Workbook(fullname=None)

Workbook connects an Excel Workbook with Python. You can create a new connection from Python with

  • a new workbook: wb = Workbook()
  • an existing workbook: wb = Workbook(r'C:\path\to\file.xlsx')

If you want to create the connection from Excel through the xlwings VBA module, use:

wb = Workbook()

Parameters:fullname (string, default None) – If you want to connect to an existing Excel file from Python, use the fullname, e.g: r'C:\path\to\file.xlsx'
activate(sheet)

Activates the given sheet.

Parameters:sheet (string or integer) – Sheet name or index.
get_selection(asarray=False)

Returns the currently selected Range from Excel as xlwings Range object.

Parameters:asarray (boolean, default False) – returns a NumPy array where empty cells are shown as nan
Return type:xlwings Range object
range(*args, **kwargs)

The range method gets and sets the Range object with the following arguments:

range('A1')          range('Sheet1', 'A1')          range(1, 'A1')
range('A1:C3')       range('Sheet1', 'A1:C3')       range(1, 'A1:C3')
range((1,2))         range('Sheet1, (1,2))          range(1, (1,2))
range((1,1), (3,3))  range('Sheet1', (1,1), (3,3))  range(1, (1,1), (3,3))
range('NamedRange')  range('Sheet1', 'NamedRange')  range(1, 'NamedRange')

If no worksheet name is provided as first argument (as name or index), it will take the range from the active sheet.

Please check the available methods/properties directly under the Range object.

Parameters:
  • asarray (boolean, default False) – returns a NumPy array where empty cells are shown as nan
  • index (boolean, default True) – Includes the index when setting a Pandas DataFrame
  • header (boolean, default True) – Includes the column headers when setting a Pandas DataFrame
Returns:

xlwings Range object

Return type:

Range

chart(*args, **kwargs)

The chart method gives access to the chart object and can be called with the following arguments:

chart(1)            chart('Sheet1', 1)              chart(1, 1)
chart('Chart 1')    chart('Sheet1', 'Chart 1')      chart(1, 'Chart 1')

If no worksheet name is provided as first argument (as name or index), it will take the Chart from the active sheet.

To insert a new Chart into Excel, create it as follows:

wb.chart().add()

Parameters:*args

Definition of sheet (optional) and chart in the above described combinations.

clear_contents(sheet=None)

Clears the content of a whole sheet but leaves the formatting.

Parameters:sheet (string or integer, default None) – Sheet name or index. If sheet is None, the active sheet is used.
clear(sheet=None)

Clears the content and formatting of a whole sheet.

Parameters:sheet (string or integer, default None) – Sheet name or index. If sheet is None, the active sheet is used.
close()

Closes the Workbook without saving it

Previous topic

Quickstart

Next topic

Range Object

This Page