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)
An app object is a member of the
appscollection:>>> 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>
- パラメータ:
visible (bool | None) -- 表示するか否かのブール値を戻すもしくは設定します。デフォルトでは値は変更されないか、オブジェクトがまだ存在していない場合にはvisible=Trueが設定されます。
spec (str | None) -- Mac-only, use the full path to the Excel application, e.g.,
/Applications/Microsoft Office 2011/Microsoft Excelor/Applications/Microsoft Excel. On Windows, if you want to change the version of Excel that xlwings talks to, go toControl Panel > Programs and FeaturesandRepairthe Office version that you want as default.
注釈
Macでは、xlwingsを使えば複数のExcelインスタンスを実行できますが、複数のExcelインスタンスの実行はMac版Excelでは公式にはサポートされていません: Windowsとは異なり、既に他のインスタンスで開かれているファイルをを開こうとする時に、Excelは読み取り専用で開くかどうかを聞きません。したがって、同じファイルが異なるインスタンスから上書きされていないかどうかを気を付ける必要があります。
- activate(steal_focus=False)¶
Exel appをアクティブにします。
- パラメータ:
steal_focus (bool) -- Trueの場合、最も前面のアプリケーションにして、フォーカスをPythonからExcelに変更します。
Added in version 0.9.0.
- alert(prompt, title=None, buttons='ok', mode=None, callback=None)¶
This corresponds to
MsgBoxin 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.- パラメータ:
prompt (str) -- The message to be displayed.
title (str | None) -- The title of the alert.
buttons (str) -- Can be either
"ok","ok_cancel","yes_no", or"yes_no_cancel".mode (str | None) -- Can be
"info"or"critical". Not supported by Google Sheets.callback (str | 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.
- 戻り値:
Nonewhen used with xlwings Server, otherwise the value of the pressed button in lowercase:"ok","cancel","yes","no".- 戻り値の型:
str | None
Added in version 0.27.13.
- property api: Any¶
Returns the native object (
pywin32orappscriptobj) of the engine being used.Added in version 0.9.0.
- calculate()¶
開いているすべてのブックで再計算します。
Added in version 0.3.6.
- property calculation: str¶
Returns or sets a calculation value that represents the calculation mode. Modes:
'manual','automatic','semiautomatic'サンプル
>>> import xlwings as xw >>> wb = xw.Book() >>> wb.app.calculation = 'manual'
バージョン 0.9.0 で変更.
- property cut_copy_mode: str | None¶
Gets or sets the status of the cut or copy mode. Accepts
Falsefor setting and returnsNone,copyorcutwhen getting the status.Added in version 0.24.0.
- property display_alerts: bool¶
デフォルト値はTrueです。コード実行中のプロンプトやアラート メッセージを非表示にするには、Falseを設定します。Falseに設定した場合、メッセージがレスポンスを必要とするときにはExcelはデフォルト値で返します。
Added in version 0.9.0.
- property enable_events: bool¶
Trueif events are enabled. Read/write boolean.Added in version 0.24.4.
- async get_selection()¶
Returns the selected cells as Range, fetched live from Excel.
Requires xlwings Lite.
Added in version 0.35.0.
- property hwnd: int | None¶
Windowハンドルを返します(Windowsのみ)。
Added in version 0.9.0.
- property interactive: bool¶
Trueif Excel is in interactive mode. If you set this property toFalse, 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を実行します。
- パラメータ:
name (str) -- e.g.,
'Module1.MyMacro'or'MyMacro'
サンプル
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
See also:
Book.macroAdded in version 0.9.0.
- property path: str¶
Returns the path to where the App is installed.
Added in version 0.28.4.
- property pid: int¶
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 object from the active sheet of the active book, see
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_templateWrites the values of all key word arguments to the
outputfile according to thetemplateand 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.
- パラメータ:
template (str | PathLike[str] | None) -- Path to your Excel template, e.g.
r'C:\Path\to\my_template.xlsx'output (str | PathLike[str] | None) -- Path to your Report, e.g.
r'C:\Path\to\my_report.xlsx'book_settings (dict[str, Any] | None) -- A dictionary of
xlwings.Bookparameters, for details see:xlwings.Book. For example:book_settings={'update_links': False}.data (Any) -- All key/value pairs that are used in the template.
Added in version 0.24.4.
- property screen_updating: bool¶
スクリプトの処理を速くするために、スクリーンの更新を止めます。スクリプトが何をしているのかが見えなくなる代わりに、速くなります。スクリプト終了時にTrueに戻すことを忘れないでください。
Added in version 0.3.3.
- property startup_path: str¶
Returns the path to
XLSTARTwhich is where the xlwings add-in gets copied to by doingxlwings addin install.Added in version 0.19.4.
- property status_bar: str | bool¶
Gets or sets the value of the status bar. Returns
Falseif Excel has control of it.Added in version 0.20.0.
- property version: VersionNumber¶
Excelのバージョン番号のオブジェクトを返します。
サンプル
>>> import xlwings as xw >>> xw.App().version VersionNumber('15.24') >>> xw.apps[10559].version.major 15
バージョン 0.9.0 で変更.
- property visible: bool¶
Gets or sets the visibility of Excel to
TrueorFalse.Added in version 0.3.3.