Table

See TableRows for row insertion and deletion. See TableColumns for column insertion and deletion. Positions passed to add() are one-based, while square-bracket lookup uses Python indexing.

class Table(*args, **options)

The table object is a member of the tables collection:

>>> # Assumes an existing workbook object named book.
>>> sheet = book.sheets[0]
>>> sheet.tables[0]  # or sheet.tables['TableName']
<Table 'Table 1' in Sheet1>

Added in version 0.21.0.

property api: Any

Returns the native object (pywin32 or appscript obj) of the engine being used.

property autofilter: AutoFilter

Returns the AutoFilter for this table.

field arguments are one-based column positions relative to the table.

Added in version 0.37.5.

property columns: TableColumns

The columns belonging to this table. Integer collection lookup is zero-based.

property data_body_range: Range | None

ヘッダー行を除く、値のrangeを表すxlwingsのrangeオブジェクトを返します。

property display_name: str

指定したTableオブジェクトの表示名を取得または設定します。

property header_row_range: Range | None

ヘッダーのrangeを表すxlwingsのrangeオブジェクトを返します。

property insert_row_range: Range | None

Returns an xlwings range object representing the row where data is going to be inserted. This is only available for empty tables, otherwise it'll return None

property name: str

Tableの名前を取得または設定します。

property parent: Sheet

tableの親オブジェクトを返します。

property range: Range

tableのxlwingsのrangeオブジェクトを返します。

resize(range)

Resize a Table by providing an xlwings range object

Added in version 0.24.4.

property rows: TableRows

Data rows of this table, excluding headers and totals.

property show_autofilter: bool

Turn the autofilter on or off by setting it to True or False (read/write boolean)

property show_headers: bool

ヘッダーの表示または非表示(読み取り/書き込み)

property show_table_style_column_stripes: bool

列方向ストライプのテーブル スタイルが使用されているかを取得または設定します(読み取り/書き込み boolean)。

property show_table_style_first_column: bool

最初の列が書式設定されているかを取得または設定します(読み取り/書き込み boolean)。

property show_table_style_last_column: bool

最後の列が書式設定されているかを取得または設定します(読み取り/書き込み boolean)。

property show_table_style_row_stripes: bool

行方向ストライプのテーブル スタイルが使用されているかを取得または設定します(読み取り/書き込み boolean)。

property show_totals: bool

集計行の表示/非表示のブール値を取得または設定します。

sort(keys, ascending=True)

Sort the table's data rows by one or more columns using Excel's native table sort.

パラメータ:
  • keys (int | Sequence[int]) -- One-based column positions within the table, in priority order.

  • ascending (bool | Sequence[bool]) -- One direction for every key, or one boolean per key.

The header and totals rows stay in place. Calculated columns, row formatting, and the table's sort state are kept. In xlwings Lite, the sort is queued until the next sync.

Added in version 0.37.6.

サンプル

table = sheet.tables["Products"]
table.sort([3, 1], [False, True])
property table_style: str

Gets or sets the table style. See Tables.add for possible values.

property totals_row_range: Range | None

集計行を表すxlwingsのrangeオブジェクトを返します。

update(data, index=True)

引数dataでExcelテーブルを更新します。現在のところ、DataFrameのみ利用できます。

バージョン 0.24.0 で変更.

パラメータ:
  • data (Any) -- Currently restricted to pandas DataFrames.

  • index (bool) -- Whether or not the index of a pandas DataFrame should be written to the Excel table.

戻り値の型:

Table

サンプル

import pandas as pd

# Assumes an existing workbook object named book.
sheet = book.sheets[0]
table_name = 'mytable'

# Sample DataFrame
nrows, ncols = 3, 3
df = pd.DataFrame(data=nrows * [ncols * ['test']],
              columns=['col ' + str(i) for i in range(ncols)])

# Hide the index, then insert a new table if it doesn't exist yet,
# otherwise update the existing one
df = df.set_index('col 0')
if table_name in [table.name for table in sheet.tables]:
sheet.tables[table_name].update(df)
else:
mytable = sheet.tables.add(source=sheet['A1'],
                           name=table_name).update(df)