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_pivot(pivot)[source]
add_print_title(n, rows_or_cols='rows')[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

Note

Deprecated: Set print titles rows or columns directly

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 (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

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

dimensions

Returns the result of calculate_dimension()

freeze_panes
get_cell_collection()[source]

Return an unordered list of the cells in this worksheet.

Note

Deprecated: Use the ws.values property

get_named_range(range_name)[source]

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

param range_name:
 named range name
type range_name:
 string
rtype:tuple[tuple[openpyxl.cell.cell.Cell]]

Note

Deprecated: Ranges are workbook objects. Use wb.defined_names[range_name]

get_squared_range(min_col, min_row, max_col, max_row)[source]
Returns a 2D array of cells. Will create any cells within the

boundaries that do not already exist

param min_col:smallest column index (1-based index)
type min_col:int
param min_row:smallest row index (1-based index)
type min_row:int
param max_col:largest column index (1-based index)
type max_col:int
param max_row:largest row index (1-based index)
type max_row:int
rtype:generator

Note

Deprecated: Use ws.iter_rows() or ws.iter_cols() depending whether you want rows or columns returned.

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)[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)
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]

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:
  • 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) – added to min_row and max_row (e.g. 4)
  • column_offset (int) – added to min_col and max_col (e.g. 3)
Return type:

generator

max_column

The maximum column index containing data (1-based)

Type:int
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]
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'
min_column

The minimum column index containing data (1-based)

Type:int
min_row

The minimium row index containing data (1-based)

Type:int
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.

print_title_cols

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

print_title_rows

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

print_titles
rows

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

Type:generator
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

Produces all cell values in the worksheet, by row

Type:generator
vba_code
openpyxl.worksheet.worksheet.flatten(results)[source]

Return cell values row-by-row

Note

Deprecated: Use the worksheet.values property

openpyxl.worksheet.worksheet.isgenerator(obj)