openpyxl.workbook.workbook module

Workbook is the top-level container for all document information.

class openpyxl.workbook.workbook.Workbook(write_only=False, iso_dates=False)[源代码]

基类:object

Workbook is the container for all other parts of the document.

property active

Get the currently active sheet or None

Type

openpyxl.worksheet.worksheet.Worksheet

add_named_range(named_range)[源代码]

Add an existing named_range to the list of named_ranges.

注解

Deprecated: Use workbook.defined_names.append

add_named_style(style)[源代码]

Add a named style

property chartsheets

A list of Chartsheets in this workbook

Type

list of openpyxl.chartsheet.chartsheet.Chartsheet

close()[源代码]

Close workbook file if open. Only affects read-only and write-only modes.

copy_worksheet(from_worksheet)[源代码]

Copy an existing worksheet in the current workbook

警告

This function cannot copy worksheets between workbooks. worksheets can only be copied within the workbook that they belong

参数

from_worksheet – the worksheet to be copied from

返回

copy of the initial worksheet

create_chartsheet(title=None, index=None)[源代码]
create_named_range(name, worksheet=None, value=None, scope=None)[源代码]

Create a new named_range on a worksheet

create_sheet(title=None, index=None)[源代码]

Create a worksheet (at an optional index).

参数
  • title (str) – optional title of the sheet

  • index (int) – optional position at which the sheet will be inserted

property data_only
property excel_base_date
get_index(worksheet)[源代码]

Return the index of the worksheet.

注解

Deprecated: Use wb.index(worksheet)

get_named_range(name)[源代码]

Return the range specified by name.

注解

Deprecated: Use workbook.defined_names[name]

get_named_ranges()[源代码]

Return all named ranges

注解

Deprecated: Use workbook.defined_names.definedName

get_sheet_by_name(name)[源代码]

Returns a worksheet by its name.

param name

the name of the worksheet to look for

type name

string

注解

Deprecated: Use wb[sheetname]

get_sheet_names()[源代码]

注解

Deprecated: Use wb.sheetnames

index(worksheet)[源代码]

Return the index of a worksheet.

property mime_type

The mime type is determined by whether a workbook is a template or not and whether it contains macros or not. Excel requires the file extension to match but openpyxl does not enforce this.

move_sheet(sheet, offset=0)[源代码]

Move a sheet or sheetname

property named_styles

List available named styles

path = '/xl/workbook.xml'
property read_only
remove(worksheet)[源代码]

Remove worksheet from this workbook.

remove_named_range(named_range)[源代码]

Remove a named_range from this workbook.

注解

Deprecated: Use del workbook.defined_names[name]

remove_sheet(worksheet)[源代码]

Remove worksheet from this workbook.

注解

Deprecated: Use wb.remove(worksheet) or del wb[sheetname]

save(filename)[源代码]

Save the current workbook under the given filename. Use this function instead of using an ExcelWriter.

警告

When creating your workbook using write_only set to True, you will only be able to call this function once. Subsequents attempts to modify or save the file will raise an openpyxl.shared.exc.WorkbookAlreadySaved exception.

property sheetnames

Returns the list of the names of worksheets in this workbook.

Names are returned in the worksheets order.

Type

list of strings

property style_names

List of named styles

template = False
property worksheets

A list of sheets in this workbook

Type

list of openpyxl.worksheet.worksheet.Worksheet

property write_only