openpyxl.worksheet.worksheet module

Worksheet is the 2nd-level container in Excel.

class openpyxl.worksheet.worksheet.Worksheet(parent, title=None)[源代码]

基类: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'
property active_cell
add_chart(chart, anchor=None)[源代码]

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

add_data_validation(data_validation)[源代码]

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)[源代码]

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

add_pivot(pivot)[源代码]
add_table(table)[源代码]

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

append(iterable)[源代码]

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)

参数

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()[源代码]

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

返回类型

string

cell(row, column, value=None)[源代码]

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.

参数
  • 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)

返回类型

openpyxl.cell.cell.Cell

property columns

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

delete_cols(idx, amount=1)[源代码]

Delete column or columns from col==idx

delete_rows(idx, amount=1)[源代码]

Delete row or rows from row==idx

property dimensions

Returns the result of calculate_dimension()

property freeze_panes
insert_cols(idx, amount=1)[源代码]

Insert column or columns before col==idx

insert_rows(idx, amount=1)[源代码]

Insert row or rows before row==idx

iter_cols(min_col=None, max_col=None, min_row=None, max_row=None, values_only=False)[源代码]

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.

参数
  • 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

返回类型

generator

iter_rows(min_row=None, max_row=None, min_col=None, max_col=None, values_only=False)[源代码]

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.

参数
  • 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

返回类型

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)[源代码]

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

注解

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 minimium row index containing data (1-based)

Type

int

move_range(cell_range, rows=0, cols=0, translate=False)[源代码]

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 page_breaks
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)[源代码]

Set printer settings

property sheet_view
property show_gridlines
property show_summary_below
property show_summary_right
property tables
unmerge_cells(range_string=None, start_row=None, start_column=None, end_row=None, end_column=None)[源代码]

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