openpyxl.worksheet.worksheet module

Worksheet is the 2nd-level container in Excel.

class openpyxl.worksheet.worksheet.Worksheet(parent, title=None)[source]

Bases: _WorkbookChild

Represents a worksheet.

Do not create worksheets yourself, use openpyxl.workbook.Workbook.create_sheet() instead

BREAK_COLUMN = 2
BREAK_NONE = 0
BREAK_ROW = 1
ORIENTATION_LANDSCAPE = 'landscape'
ORIENTATION_PORTRAIT = 'portrait'
PAPERSIZE_A3 = '8'
PAPERSIZE_A4 = '9'
PAPERSIZE_A4_SMALL = '10'
PAPERSIZE_A5 = '11'
PAPERSIZE_EXECUTIVE = '7'
PAPERSIZE_LEDGER = '4'
PAPERSIZE_LETTER = '1'
PAPERSIZE_LETTER_SMALL = '2'
PAPERSIZE_STATEMENT = '6'
PAPERSIZE_TABLOID = '3'
SHEETSTATE_HIDDEN = 'hidden'
SHEETSTATE_VERYHIDDEN = 'veryHidden'
SHEETSTATE_VISIBLE = 'visible'
property active_cell
add_chart(chart, anchor=None)[source]

Add a chart to the sheet Optionally provide a cell for the top-left anchor

add_data_validation(data_validation)[source]

Add a data-validation object to the sheet. The data-validation object defines the type of data-validation to be applied and the cell or range of cells it should apply to.

add_image(img, anchor=None)[source]

Add an image to the sheet. Optionally provide a cell for the top-left anchor

add_pivot(pivot)[source]
add_table(table)[source]

Check for duplicate name in definedNames and other worksheet tables before adding table.

append(iterable)[source]

Appends a group of values at the bottom of the current sheet.

  • If it’s a list: all values are added in order, starting from the first column

  • If it’s a dict: values are assigned to the columns indicated by the keys (numbers or letters)

Parameters:

iterable (list|tuple|range|generator or dict) – list, range or generator, or dict containing values to append

Usage:

  • append([‘This is A1’, ‘This is B1’, ‘This is C1’])

  • or append({‘A’ : ‘This is A1’, ‘C’ : ‘This is C1’})

  • or append({1 : ‘This is A1’, 3 : ‘This is C1’})

Raise:

TypeError when iterable is neither a list/tuple nor a dict

property array_formulae

Returns a dictionary of cells with array formulae and the cells in array

calculate_dimension()[source]

Return the minimum bounding range for all cells containing data (ex. ‘A1:M24’)

Return type:

string

cell(row, column, value=None)[source]

Returns a cell object based on the given coordinates.

Usage: cell(row=15, column=1, value=5)

Calling cell creates cells in memory when they are first accessed.

Parameters:
  • row (int) – row index of the cell (e.g. 4)

  • column (int) – column index of the cell (e.g. 3)

  • value (numeric or time or string or bool or none) – value of the cell (e.g. 5)

Return type:

openpyxl.cell.cell.Cell

property column_groups

Return a list of column ranges where more than one column

property columns

Produces all cells in the worksheet, by column (see iter_cols())

delete_cols(idx, amount=1)[source]

Delete column or columns from col==idx

delete_rows(idx, amount=1)[source]

Delete row or rows from row==idx

property dimensions

Returns the result of calculate_dimension()

property freeze_panes
insert_cols(idx, amount=1)[source]

Insert column or columns before col==idx

insert_rows(idx, amount=1)[source]

Insert row or rows before row==idx

iter_cols(min_col=None, max_col=None, min_row=None, max_row=None, values_only=False)[source]

Produces cells from the worksheet, by column. Specify the iteration range using indices of rows and columns.

If no indices are specified the range starts at A1.

If no cells are in the worksheet an empty tuple will be returned.

Parameters:
  • min_col (int) – smallest column index (1-based index)

  • min_row (int) – smallest row index (1-based index)

  • max_col (int) – largest column index (1-based index)

  • max_row (int) – largest row index (1-based index)

  • values_only (bool) – whether only cell values should be returned

Return type:

generator

iter_rows(min_row=None, max_row=None, min_col=None, max_col=None, values_only=False)[source]

Produces cells from the worksheet, by row. Specify the iteration range using indices of rows and columns.

If no indices are specified the range starts at A1.

If no cells are in the worksheet an empty tuple will be returned.

Parameters:
  • min_col (int) – smallest column index (1-based index)

  • min_row (int) – smallest row index (1-based index)

  • max_col (int) – largest column index (1-based index)

  • max_row (int) – largest row index (1-based index)

  • values_only (bool) – whether only cell values should be returned

Return type:

generator

property max_column

The maximum column index containing data (1-based)

Type:

int

property max_row

The maximum row index containing data (1-based)

Type:

int

merge_cells(range_string=None, start_row=None, start_column=None, end_row=None, end_column=None)[source]

Set merge on a cell range. Range is a cell range (e.g. A1:E1)

property merged_cell_ranges

Return a copy of cell ranges

Note

Deprecated: Use ws.merged_cells.ranges

mime_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml'
property min_column

The minimum column index containing data (1-based)

Type:

int

property min_row

The minimum row index containing data (1-based)

Type:

int

move_range(cell_range, rows=0, cols=0, translate=False)[source]

Move a cell range by the number of rows and/or columns: down if rows > 0 and up if rows < 0 right if cols > 0 and left if cols < 0 Existing cells will be overwritten. Formulae and references will not be updated.

property print_area

The print area for the worksheet, or None if not set. To set, supply a range like ‘A1:D4’ or a list of ranges.

property print_title_cols

Columns to be printed at the left side of every page (ex: ‘A:C’)

property print_title_rows

Rows to be printed at the top of every page (ex: ‘1:3’)

property print_titles
property rows

Produces all cells in the worksheet, by row (see iter_rows())

Type:

generator

property selected_cell
set_printer_settings(paper_size, orientation)[source]

Set printer settings

property sheet_view
property show_gridlines
property tables
unmerge_cells(range_string=None, start_row=None, start_column=None, end_row=None, end_column=None)[source]

Remove merge on a cell range. Range is a cell range (e.g. A1:E1)

property values

Produces all cell values in the worksheet, by row

Type:

generator

openpyxl.worksheet.worksheet.isgenerator(obj)