LoginSignup
3
4

More than 3 years have passed since last update.

Automation Anywhereでpythonの実行結果を取得する(VBScriptを利用して外部プログラムの応答結果を処理する例)

Last updated at Posted at 2018-02-28

pythonプログラムをRPAから呼び出し、その実行結果を次のステップで利用したいケースがあります。
pythonによる画像検索や機械学習の実行結果を取得できたら、RPAによる自動化の適用範囲がずいぶんと広がるように思います。
そこでVBScriptからpythonを実行し、処理結果を受け取る方法を調べてみました。

【前提】
python 3.x
IBM RPA 10.6

【解説】
はじめにpythonのコードです。ここではファイル名をhello.pyとします。
printの出力内容を戻すことにします。

def message():
    print("hello rpa")

if __name__ == '__main__':
    message()

つづいて、pythonを呼び出すVBScriptのコードです。ここではファイル名をexecutePgm.vbsとします。

Option Explicit

Dim exeFile,pgmFile,returnMessage
Dim ShellObj,ShellExec

' 引数の処理
if WScript.Arguments.Count = 0 then
    returnMessage = -1
    WScript.StdOut.Writeline returnMessage ' Run Scriptに戻り値を渡す
    WScript.Quit(returnMessage)
end If

exeFile = WScript.Arguments(0) 'ex:python.exe
pgmFile = WScript.Arguments(1) 'ex:hello.py

Set ShellObj = WScript.CreateObject("WScript.Shell")
Set ShellExec = ShellObj.Exec(exeFile + " "+ pgmFile)

Do Until ShellExec.StdOut.AtEndOfStream '最後までループ
    returnMessage = returnMessage & ShellExec.StdOut.ReadLine 'pythonのprint出力行を読み取る
Loop

WScript.StdOut.Writeline returnMessage ' Run Scriptに戻り値を渡す
WScript.Quit(0)

VBScriptの実行結果をRun Scriptに戻すためには、WScript.StdOut.Writelineを利用します。WScript.Quitでは戻り値を設定できないため、注意してください。

WScript.StdOut.Writeline returnMessage ' Run Scriptに戻り値を渡す

最後に、vbsを呼び出すIBM RPA (Automation Anywhere)のコードです。
IBM RPAではRun Scriptコマンドを利用してVBScriptを呼び出すことができます。
image
executePgm.vbsの引数としてhello.pyを渡します。
その出力結果を returnMessage 変数に設定します。
image

実行結果です。
image

【リンク】
https://github.com/daikanmurata/rpa_vbs_python.git

pythonをonewayで呼び出す方法はこちらを参照ください。Automation Anywhere から PowerShell やPython などの外部プログラムを呼び出す

最後までお読みいただきましてありがとうございました。

3
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4