Sheet
- class Sheet(sheet=None)
A sheet object is a member of the
sheetscollection:>>> # Assumes an existing workbook object named book. >>> book.sheets[0] <Sheet [Book1]Sheet1> >>> book.sheets['Sheet1'] <Sheet [Book1]Sheet1> >>> book.sheets.add() <Sheet [Book1]Sheet2>
バージョン 0.9.0 で変更.
- property api: Any
Returns the native object (
pywin32orappscriptobj) of the engine being used.In xlwings Lite, this is the remote backend's internal worksheet data dictionary. It does not expose the Office.js API; use the public xlwings methods and properties for workbook operations.
Added in version 0.9.0.
- autofit(axis=None)
シート全体の列、行または両方の幅を自動調整します。
- パラメータ:
axis (str | None) -- To autofit rows, use
"rows"or"r". To autofit columns, use"columns"or"c". To autofit rows and columns, provide no arguments.
サンプル
>>> # Assumes an existing workbook object named book. >>> book.sheets['Sheet1'].autofit('c') >>> book.sheets['Sheet1'].autofit('r') >>> book.sheets['Sheet1'].autofit()
Added in version 0.2.3.
- property book: Book
このシートを含むBookを返します。読み取り専用。
- property cells: Range
Sheetオブジェクト上のすべてのセルを表すRangeオブジェクトを返します(現在使っているセルではありません)。
Added in version 0.9.0.
- clear_formats()
Clears the format of the whole sheet but leaves the content.
Added in version 0.26.2.
- property comments: Comments
Threaded comments on this worksheet. In xlwings Lite, use
await get_comments().
- copy(before=None, after=None, name=None)
同一ブック内または新規ブックにシートをコピーします。デフォルトでは、同一ブックの一番右側にコピーします。コピー後のシートを返します。
Added in version 0.22.0.
- パラメータ:
- 戻り値の型:
サンプル
# 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])
- property freeze_panes: FreezePanes
Interface to freeze/unfreeze panes.
サンプル
>>> mysheet.freeze_panes.freeze_at("A1") >>> mysheet.freeze_panes.freeze_at(mysheet["A1"]) >>> mysheet.freeze_panes.freeze_at("A:A") >>> mysheet.freeze_panes.freeze_at("1:1") >>> mysheet.freeze_panes.unfreeze()
- async get_used_range(values_only=False)
Returns the used range fetched from the current worksheet state, or
Noneif the worksheet is empty according tovalues_only.- パラメータ:
values_only (bool) -- If
True, only cells with values count as used. IfFalse, cells with values or formatting count as used.- 戻り値の型:
Range | None
Unlike this method,
used_rangeis a values-only snapshot in xlwings Lite and returnsA1for an empty worksheet for backward compatibility.Requires xlwings Lite.
Added in version 0.37.5.
- async load(values=None)
(Re)loads the sheet's data from Excel on demand.
Like
Book.load, this loads only metadata by default on an async book and everything (including values) on a regular book. Passvalues=Trueto also load this sheet's cell values.Requires xlwings Lite.
- 戻り値の型:
- move(before=None, after=None)
Move a sheet within its current Book.
Provide exactly one of
beforeorafter. Both the sheet being moved and the target sheet must belong to the same Book.- パラメータ:
サンプル
book.sheets["Sheet3"].move(after=book.sheets["Sheet1"])
Added in version 0.37.5.
- property names: Names
このシート内のすべての名前("SheetName!" から始まるよう定義された名前)を表すnamesコレクションを返します。
Added in version 0.9.0.
- property notes: Notes
Notes on this worksheet, indexed by cell address or position.
- property page_setup: PageSetup
Returns a PageSetup object.
Added in version 0.24.2.
- property pivot_tables: PivotTables
See
PivotTablesAdded in version 0.37.3.
- range(cell1, cell2=None)
Returns a Range object from the active sheet of the active book, see
Range.Added in version 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.
Added in version 0.22.0.
- パラメータ:
data (Any) -- All key/value pairs that are used in the template.
サンプル
>>> # Assumes an existing workbook object named book. >>> book.sheets[0]['A1:A2'].value = '{{ myvar }}' >>> book.sheets[0].render_template(myvar='test')
- property show_gridlines: bool
Gets or sets whether the sheet displays gridlines. This only affects what is shown on screen, not what is printed.
With classic xlwings (Python running locally), the sheet must be (temporarily) activated by xlwings, which means that you can't use this with hidden sheets. xlwings Server and xlwings Lite don't have this restriction.
サンプル
>>> mysheet.show_gridlines = False
Added in version 0.37.3.
- to_html(path=None)
Export a Sheet as HTML page.
- パラメータ:
path (str | PathLike[str] | None) -- Path where you want to save the HTML file. Defaults to Sheet name in the current working directory.
Added in version 0.28.1.
- to_pdf(path=None, layout=None, show=False, quality='standard')
シートをPDFファイルで出力します。
- パラメータ:
path (str | PathLike[str] | None) -- PDFファイルのパス。デフォルトはワークブックと同じディレクトリーにシート名で保存します。未保存のワークブックの場合は、カレント ワーキング ディレトリーに保存します。
layout (str | PathLike[str] | 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). New in version 0.24.3.show (bool) -- Once created, open the PDF file with the default application. New in version 0.24.6.
quality (str) -- Quality of the PDF file. Can either be
'standard'or'minimum'. New in version 0.26.2.
- 戻り値の型:
str
サンプル
>>> # Assumes an existing workbook object named book. >>> sheet = book.sheets[0] >>> sheet['A1'].value = 'PDF' >>> sheet.to_pdf()
See also
xlwings.Book.to_pdfAdded in version 0.22.3.
- property used_range: Range
シートで使用しているRange。
In xlwings Lite, this is a values-only snapshot and returns
A1for an empty worksheet. Useget_used_range()for a current formatting-aware result that returnsNonewhen the worksheet is empty.Added in version 0.13.0.