Sheet#

class Sheet(sheet=None, impl=None)#

一个sheet对象是sheets集合中的一员:

>>> import xlwings as xw
>>> wb = xw.Book()
>>> wb.sheets[0]
<Sheet [Book1]Sheet1>
>>> wb.sheets['Sheet1']
<Sheet [Book1]Sheet1>
>>> wb.sheets.add()
<Sheet [Book1]Sheet2>

在 0.9.0 版本发生变更.

activate()#

激活sheet并把它返回。

property api#

返回正在使用的引擎的原生对象( pywin32appscript 对象)。

在 0.9.0 版本加入.

autofit(axis=None)#

在整个工作表中对行、列或者两者同时根据内容进行自适应。

Arguments#

axisstring, default None
  • 要做行自适应,用 rowsr

  • 要做列自适应,用 columnsc

  • 同时做行和列的自适应,不需要参数。

示例#

>>> import xlwings as xw
>>> wb = xw.Book()
>>> wb.sheets['Sheet1'].autofit('c')
>>> wb.sheets['Sheet1'].autofit('r')
>>> wb.sheets['Sheet1'].autofit()

在 0.2.3 版本加入.

property book#

返回指定工作表所属的工作簿。只读。

property cells#

返回一个代表工作表上所有单元格的区域对象(不仅仅是那些正在使用中的单元格)。

在 0.9.0 版本加入.

property charts#

参见 Charts

在 0.9.0 版本加入.

clear()#

重新格式化工作表,清除所有内容及格式。

clear_contents()#

清除工作表的所有内容但是保留原有格式。

clear_formats()#

Clears the format of the whole sheet but leaves the content.

在 0.26.2 版本加入.

copy(before=None, after=None, name=None)#

Copy a sheet to the current or a new Book. By default, it places the copied sheet after all existing sheets in the current Book. Returns the copied sheet.

在 0.22.0 版本加入.

Arguments#

beforesheet object, default None

The sheet object before which you want to place the sheet

aftersheet object, default None

The sheet object after which you want to place the sheet, by default it is placed after all existing sheets

namestr, default None

The sheet name of the copy

返回#

Sheet object: Sheet

The copied sheet

示例#

# Create two books and add a value to the first sheet of the first book
first_book = xw.Book()
second_book = xw.Book()
first_book.sheets[0]['A1'].value = 'some value'

# Copy to same Book with the default location and name
first_book.sheets[0].copy()

# Copy to same Book with custom sheet name
first_book.sheets[0].copy(name='copied')

# Copy to second Book requires to use before or after
first_book.sheets[0].copy(after=second_book.sheets[0])
delete()#

删除工作表。

在 0.6.0 版本加入.

property index#

返回工作表的索引值(按照Excel的方式,从1开始的)。

property name#

取得或者设置工作表的名字。

property names#

返回所有名字与本工作表有关的命名区域的集合(名字定义中包含”SheetName!” (工作表名!)前缀)。

在 0.9.0 版本加入.

property page_setup#

Returns a PageSetup object.

在 0.24.2 版本加入.

property pictures#

参见 Pictures

在 0.9.0 版本加入.

range(cell1, cell2=None)#

从活动工作簿的活动工作表中返回一个区域对象, 参见 Range()

在 0.9.0 版本加入.

render_template(**data)#

This method requires xlwings PRO.

Replaces all Jinja variables (e.g {{ myvar }}) in the sheet with the keyword argument that has the same name. Following variable types are supported:

strings, numbers, lists, simple dicts, NumPy arrays, Pandas DataFrames, PIL Image objects that have a filename and Matplotlib figures.

在 0.22.0 版本加入.

参数#

data: kwargs

All key/value pairs that are used in the template.

示例#

>>> import xlwings as xw
>>> book = xw.Book()
>>> book.sheets[0]['A1:A2'].value = '{{ myvar }}'
>>> book.sheets[0].render_template(myvar='test')
select()#

选定工作表。只能在活动工作簿中选择。

在 0.9.0 版本加入.

property shapes#

参见 Shapes

在 0.9.0 版本加入.

property tables#

See Tables

在 0.21.0 版本加入.

to_html(path=None)#

Export a Sheet as HTML page.

参数#

pathstr or path-like, default None

Path where you want to save the HTML file. Defaults to Sheet name in the current working directory.

在 0.28.1 版本加入.

to_pdf(path=None, layout=None, show=False, quality='standard')#

Exports the sheet to a PDF file.

参数#

pathstr or path-like object, default None

Path to the PDF file, defaults to the name of the sheet in the same directory of the workbook. For unsaved workbooks, it defaults to the current working directory instead.

layoutstr or path-like object, default None

This argument requires xlwings PRO.

Path to a PDF file on which the report will be printed. This is ideal for headers and footers as well as borderless printing of graphics/artwork. The PDF file either needs to have only 1 page (every report page uses the same layout) or otherwise needs the same amount of pages as the report (each report page is printed on the respective page in the layout PDF).

在 0.24.3 版本加入.

showbool类型, 缺省值为False

Once created, open the PDF file with the default application.

在 0.24.6 版本加入.

qualitystr, default 'standard'

Quality of the PDF file. Can either be 'standard' or 'minimum'.

在 0.26.2 版本加入.

示例#

>>> wb = xw.Book()
>>> sheet = wb.sheets[0]
>>> sheet['A1'].value = 'PDF'
>>> sheet.to_pdf()

See also xlwings.Book.to_pdf()

在 0.22.3 版本加入.

property used_range#

工作表中用过的区域。

返回#

xw.Range

在 0.13.0 版本加入.

property visible#

Gets or sets the visibility of the Sheet (bool).

在 0.21.1 版本加入.