openpyxl.worksheet.worksheet module

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

Bases: openpyxl.workbook.child._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'
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_print_title(*args, **kwargs)[source]

Print Titles are rows or columns that are repeated on each printed sheet. This adds n rows or columns at the top or left of the sheet

add_table(table)[source]
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
calculate_dimension()[source]

Return the minimum bounding range for all cells containing data.

cell(coordinate=None, row=None, column=None, 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)
  • coordinate (string) – coordinates of the cell (e.g. ‘B12’)
  • value (numeric or time or string or bool or none) – value of the cell (e.g. 5)
Raise:

InsufficientCoordinatesException when neither row nor column are not given

Return type:

openpyxl.cell.Cell

columns

Iterate over all columns in the worksheet

dimensions
freeze_panes
get_cell_collection(*args, **kwargs)[source]

Return an unordered list of the cells in this worksheet.

get_named_range(*args, **kwargs)[source]

Returns a 2D array of cells, with optional row and column offsets.

Parameters:range_name (string) – named range name
Return type:tuple[tuple[openpyxl.cell.Cell]]
get_squared_range(*args, **kwargs)[source]

Returns a 2D array of cells. Will create any cells within the boundaries that do not already exist

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) – smallest row index (1-based index)
Return type:

generator

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

Returns all cells in the worksheet from the first row as columns.

If no boundaries are passed in the cells will start 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) – smallest row index (1-based index)
Return type:

generator

iter_rows(range_string=None, min_row=None, max_row=None, min_col=None, max_col=None, row_offset=0, column_offset=0)[source]

Return cells from the worksheet as rows. Boundaries for the cells can be passed in either as indices of rows and columns.

If no boundaries are passed in the cells will start at A1.

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

Additional rows and columns can be created using offsets.

Parameters:
  • range_string (string) – range string (e.g. ‘A1:B2’) deprecated
  • 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) – smallest row index (1-based index)
  • row_offset (int) – additional rows (e.g. 4)
  • column_offset (int) – additional columns (e.g. 3)
Return type:

generator

max_column

Get the largest value for column currently stored.

Return type:int
max_row

Returns the maximum row index containing data

Return 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)

merged_cell_ranges

Return a copy of cell ranges

merged_cells

Utility for checking whether a cell has been merged or not

mime_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml'
min_column
min_row
point_pos(*args, **kwargs)[source]

tells which cell is under the given coordinates (in pixels) counting from the top-left corner of the sheet. Can be used to locate images and charts on the worksheet

print_area

Return the print area for the worksheet, if set

print_title_cols
print_title_rows
print_titles
rows

Iterate over all rows in the worksheet

selected_cell
set_printer_settings(paper_size, orientation)[source]

Set printer settings

sheet_view
show_gridlines
show_summary_below
show_summary_right
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)

values

Return the values of all the cells, row by row

vba_code
openpyxl.worksheet.worksheet.flatten(*args, **kwargs)[source]

Return cell values row-by-row

openpyxl.worksheet.worksheet.isgenerator(obj)