App

class App(visible=None, spec=None, add_book=True, impl=None)

An app corresponds to an Excel instance and should normally be used as context manager to make sure that everything is properly cleaned up again and to prevent zombie processes. New Excel instances can be fired up like so:

import xlwings as xw

with xw.App() as app:
    print(app.books)

appオブジェクトは apps コレクションのメンバーです:

>>> xw.apps
Apps([<Excel App 1668>, <Excel App 1644>])
>>> xw.apps[1668]  # get the available PIDs via xw.apps.keys()
<Excel App 1668>
>>> xw.apps.active
<Excel App 1668>

Parameters

visiblebool, default None

表示するか否かのブール値を戻すもしくは設定します。デフォルトでは値は変更されないか、オブジェクトがまだ存在していない場合にはvisible=Trueが設定されます。

specstr, default None

Macのみ、Excelアプリケーションへのフルパスを指定します。例えば、/Applications/Microsoft Office 2011/Microsoft Excel/Applications/Microsoft Excel

Windowsでは、xlwingsと連携するExcelのバージョンを変更したい場合、コントロールパネル > プログラムと機能 からデフォルトで使いたいオフィスのバージョンに 変更 します。

注釈

Macでは、xlwingsを使えば複数のExcelインスタンスを実行できますが、複数のExcelインスタンスの実行はMac版Excelでは公式にはサポートされていません: Windowsとは異なり、既に他のインスタンスで開かれているファイルをを開こうとする時に、Excelは読み取り専用で開くかどうかを聞きません。したがって、同じファイルが異なるインスタンスから上書きされていないかどうかを気を付ける必要があります。

activate(steal_focus=False)

Exel appをアクティブにします。

Parameters

steal_focusbool, default False

Trueの場合、最も前面のアプリケーションにして、フォーカスをPythonからExcelに変更します。

Added in version 0.9.0.

alert(prompt, title=None, buttons='ok', mode=None, callback=None)

This corresponds to MsgBox in VBA, shows an alert/message box and returns the value of the pressed button. For xlwings Server, instead of returning a value, the function accepts the name of a callback to which it will supply the value of the pressed button.

Parameters

promptstr, default None

The message to be displayed.

titlestr, default None

The title of the alert.

buttonsstr, default "ok"

Can be either "ok", "ok_cancel", "yes_no", or "yes_no_cancel".

modestr, default None

Can be "info" or "critical". Not supported by Google Sheets.

callbackstr, default None

Only used by xlwings Server: you can provide the name of a function that will be called with the value of the pressed button as argument. The function has to exist on the client side, i.e., in VBA or JavaScript.

Returns

button_value: str or None

Returns None when used with xlwings Server, otherwise the value of the pressed button in lowercase: "ok", "cancel", "yes", "no".

Added in version 0.27.13.

property api

使用しているエンジンのネイティブ オブジェクト(pywin32 オブジェクトまたは appscript オブジェクト)を返します。

Added in version 0.9.0.

property books

現在開かれているすべてのBookオブジェクトのコレクション。

Added in version 0.9.0.

calculate()

開いているすべてのブックで再計算します。

Added in version 0.3.6.

property calculation

計算モードを表す値を返すまたは設定します。 計算モード: 'manual''automatic''semiautomatic'

>>> import xlwings as xw
>>> wb = xw.Book()
>>> wb.app.calculation = 'manual'

バージョン 0.9.0 で変更.

property cut_copy_mode

Gets or sets the status of the cut or copy mode. Accepts False for setting and returns None, copy or cut when getting the status.

Added in version 0.24.0.

property display_alerts

デフォルト値はTrueです。コード実行中のプロンプトやアラート メッセージを非表示にするには、Falseを設定します。Falseに設定した場合、メッセージがレスポンスを必要とするときにはExcelはデフォルト値で返します。

Added in version 0.9.0.

property enable_events

True if events are enabled. Read/write boolean.

Added in version 0.24.4.

property hwnd

Windowハンドルを返します(Windowsのみ)。

Added in version 0.9.0.

property interactive

True if Excel is in interactive mode. If you set this property to False, Excel blocks all input from the keyboard and mouse (except input to dialog boxes that are displayed by your code). Read/write Boolean. NOTE: Not supported on macOS.

Added in version 0.24.4.

kill()

プロセスをキルしてExcelアプリを強制的に終了します。

Added in version 0.9.0.

macro(name)

特定のワークブックではない、アドイン等に含まれるExcel VBAのSubやFunctionを実行します。

Arguments

nameName of Sub or Function with or without module name,

e.g., 'Module1.MyMacro' or 'MyMacro'

次のVBA関数は

Function MySum(x, y)
    MySum = x + y
End Function

このように使うことができます:

>>> import xlwings as xw
>>> app = xw.App()
>>> my_sum = app.macro('MySum')
>>> my_sum(1, 2)
3

Types are supported too:

Function MySum(x as integer, y as integer)
    MySum = x + y
End Function
>>> import xlwings as xw
>>> app = xw.App()
>>> my_sum = app.macro('MySum')
>>> my_sum(1, 2)
3

However typed arrays are not supported. So the following won't work

Function MySum(arr() as integer)
    ' code here
End Function

Book.macro() をご覧ください。

Added in version 0.9.0.

property path

Returns the path to where the App is installed.

Added in version 0.28.4.

property pid

appのPIDを返します。

Added in version 0.9.0.

properties(**kwargs)

Context manager that allows you to easily change the app's properties temporarily. Once the code leaves the with block, the properties are changed back to their previous state. Note: Must be used as context manager or else will have no effect. Also, you can only use app properties that you can both read and write.

import xlwings as xw
app = App()

# Sets app.display_alerts = False
with app.properties(display_alerts=False):
    # do stuff

# Sets app.calculation = 'manual' and app.enable_events = True
with app.properties(calculation='manual', enable_events=True):
    # do stuff

# Makes sure the status bar is reset even if an error happens in the with block
with app.properties(status_bar='Calculating...'):
    # do stuff

Added in version 0.24.4.

quit()

ワークブックを保存せずにアプリケーションを終了します。

Added in version 0.3.3.

range(cell1, cell2=None)

アクティブブックのアクティブシートのRangeオブジェクトを返します。Range() を参照。

Added in version 0.9.0.

render_template(template=None, output=None, book_settings=None, **data)

This function requires xlwings PRO.

This is a convenience wrapper around mysheet.render_template

Writes the values of all key word arguments to the output file according to the template and the variables contained in there (Jinja variable syntax). Following variable types are supported:

strings, numbers, lists, simple dicts, NumPy arrays, Pandas DataFrames, pictures and Matplotlib/Plotly figures.

Parameters

template: str or path-like object

Path to your Excel template, e.g. r'C:\Path\to\my_template.xlsx'

output: str or path-like object

Path to your Report, e.g. r'C:\Path\to\my_report.xlsx'

book_settings: dict, default None

A dictionary of xlwings.Book parameters, for details see: xlwings.Book. For example: book_settings={'update_links': False}.

data: kwargs

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

Returns

wb: xlwings Book

Added in version 0.24.4.

property screen_updating

スクリプトの処理を速くするために、スクリーンの更新を止めます。スクリプトが何をしているのかが見えなくなる代わりに、速くなります。スクリプト終了時にTrueに戻すことを忘れないでください。

Added in version 0.3.3.

property selection

選択しているセルをRangeとして返します。

Added in version 0.9.0.

property startup_path

xlwings addin install コマンドでxlwingsアドインをコピーした先の``XLSTART`` フォルダーのパスを返します。

Added in version 0.19.4.

property status_bar

ステータスバーの値を取得または設定します。Excelがステータスバーをコントロールしている場合には False を返します。

Added in version 0.20.0.

property version

Excelのバージョン番号のオブジェクトを返します。

>>> import xlwings as xw
>>> xw.App().version
VersionNumber('15.24')
>>> xw.apps[10559].version.major
15

バージョン 0.9.0 で変更.

property visible

Excelを表示するか否かを True または False で取得または設定します。

Added in version 0.3.3.