ホーム / Pythonスクリプト / Python Femtet ユーティリティパッケージ関数
Python Femtet ユーティリティパッケージ関数
説明
- PythonスクリプトからFemtetの起動、別アプリケーションの起動等ができます。
- Femtetインタフェースクラスの名称が定義されています。これを利用してインスタンスの作成を行うと便利です。
関数の使用方法
- Pythonの動作環境を準備する。
- Pythonスクリプトで、Python Femtet ユーティリティパッケージをインポートする。
例: from femtetutils import util, const
- Pythonスクリプトで関数を使用する。
例: util.auto_execute_femtet()
関数のログ出力について
- 関数を使用すると、ログが出力されることがあります。関数から出力されるログを停止したい場合は、以下の手順を実施してください。
- 関数呼び出し元のスクリプトファイル(pyファイル)で、下記の処理を追加する。
from logging import disable
if __name__ == '__main__':
disable() # Femtetユーティリティパッケージ関数が出すログを出力しない
プロセスIDとウィンドウハンドルについて
- マクロを実行すると、CFemtetクラスのhWndプロパティ(Femtetのウィンドウハンドル)には0以外の値が設定される。
- close_femtet()関数でウィンドウハンドルを指定して、マクロ実行に使用したFemtetを閉じることができる。
- get_process_id_from_hwnd()関数で、ウィンドウハンドルを指定するとプロセスIDが取得できる。
- プロセスを起動するとそのプロセスにはIDが割り当てられる。
- close_process()関数で、プロセスを閉じることができる。
関数リスト
| 関数名 | 概要 |
| auto_execute_femtet | Femtet.exeの起動(未使用のFemtetが存在しない場合) |
| execute_femtet | Femtet.exeの起動 |
| close_femtet | 指定したウィンドウハンドルのFemtet.exeを閉じる |
| get_last_executed_femtet_process_id | 直前に起動したFemtet.exeのプロセスID取得 |
| get_femtet_exe_path | Femtet.exeのPathを取得 |
| is_femtet_active | Femtet.exeが起動中か |
| execute_process | プロセスの起動 |
| close_process | 指定したプロセスIDのプロセスを閉じる(強制終了) |
| get_last_executed_process_id | 直前に起動したプロセスのプロセスID取得 |
| get_process_id_from_hwnd | プロセスIDの取得 |
auto_execute_femtet(wait_second=60)
[概要] Femtet.exeの起動(未使用のFemtetが存在しない場合)
[詳細説明] Femtetが1つも起動していない、または既存のFemtetがすべて使用中の場合にFemtetを起動
[引数] wait_second: 起動まで指定時間待機します(秒)
[引数の型] wait_second: int(秒)
[戻り値] True:未使用のFemtetが起動中または新規でFemtetを起動した, False:Femtetが起動できなかった
[戻り値の型] bool
[使用例]
from femtetutils import util, const util.auto_execute_femtet()
close_femtet(hwnd, wait_second=60, isTerminate=True)
[概要] 指定したウィンドウハンドルのFemtet.exeを閉じる
[詳細説明] Femtet.exeのウィンドウハンドルを引数に指定し、対象のFemtet.exeを閉じる。Femtet.Exit() 関数と異なり、強制終了を行うことができる。
[引数] hwnd:Femtetのウィンドウハンドル
補足:CFemtet.hWndでマクロに関連付けされたウィンドウハンドルを取得可能。
[引数の型] hwnd: int
[引数2] wait_second: 閉じるまで待機を行う最大時間(秒)
[引数2の型] wait_second: int(秒)
[引数3] isTerminate: 閉じることが出来ない場合、強制終了するか True:強制終了する, False:強制終了しない
[引数3の型] isTerminate: bool
[戻り値] True:Femtetを閉じた, False:Femtetを時間内に閉じることができなかった
[戻り値の型] bool
[使用例]
import sys from femtetutils import util, const from win32com.client import Dispatch, constants util.auto_execute_femtet() Femtet = Dispatch(const.CFemtet) if Femtet.OpenNewProject() == False:
print(Femtet.LastErrorMsg)
sys.exit() util.close_femtet(Femtet.hWnd)
close_process(process_id)
[概要] 指定したプロセスIDのプロセスを閉じる(強制終了)
[詳細説明] プロセスIDを指定すると対象のプロセスを閉じる(強制終了)。
[引数] process_id: プロセスID
[引数の型] process_id: int
[戻り値] True:プロセスを強制終了した, False:強制終了処理中にエラーが発生した
[戻り値の型] bool
[使用例]
import time from femtetutils import util, const ret, code = util.execute_process("notepad.exe") time.sleep(10) util.close_process(util.get_last_executed_process_id())
execute_femtet()
[概要] Femtet.exeの起動
[詳細説明] Femtet.exeを起動する
[戻り値] True:プロセスが起動中, False:プロセスが起動できなかった
[戻り値の型] bool
[使用例]
from femtetutils import util, const util.execute_femtet()
execute_process(command, timeout_second=None)
[概要] プロセスの起動
[詳細説明] 指定したプロセスを起動する。
- 引数で時間を指定しない場合は、プロセスを非同期実行
- 引数で時間を指定した場合は、プロセスが終了するまで待機を行う。
待機時間を超えてもプロセスが終了していない場合はエラーとなる。
[引数] command: 起動したいプロセスのパス
例: "notepad.exe"
[引数の型] command: str
[引数] timeout_second: 指定時間になるまで、プロセスの終了を待ちます(秒)
[引数の型] timeout_second: int(秒)
[戻り値] True:プロセスが起動中, False:プロセスが起動できなかった
[戻り値の型] bool
[戻り値2] [起動成功] プロセスからの終了コード
[エラーの場合] -1:タイムアウト, -2:プロセス例外エラー発生
[戻り値2]の型 int
[使用例]
from femtetutils import util, const ret, code = util.execute_process("notepad.exe")
get_femtet_exe_path()
[概要] Femtet.exeのPathを取得
[詳細説明] レジストリ情報から、Femtet.exeのパスを取得する関数
[戻り値] Femtet.exeのパス
例: "C:\Program Files\Femtet_Ver2023_64bit\Program\Femtet.exe"
[戻り値の型] str
[使用例]
from femtetutils import util, const femtetpath = util.get_femtet_exe_path() print(femtetpath)
get_last_executed_femtet_process_id()
[概要] 直前に起動したFemtet.exeのプロセスID取得
[詳細説明] execute_femtet()で起動したプロセスIDを取得する
※直前に起動したプロセスのみ取得可能
[戻り値] プロセスID(存在しない場合は0)
[戻り値の型] int
[使用例]
import time from femtetutils import util, const if util.execute_femtet() == True:
procid = util.get_last_executed_femtet_process_id()
time.sleep(20)
print(procid)
get_last_executed_process_id()
[概要] 直前に起動したプロセスのプロセスID取得
[詳細説明] execute_process()で起動したプロセスIDを取得する
※直前に起動したプロセスのみ取得可能
[戻り値] プロセスID(存在しない場合は0)
[戻り値の型] int
[使用例]
import time from femtetutils import util, const ret, code = util.execute_process("notepad.exe") time.sleep(10) util.close_process(util.get_last_executed_process_id())
get_process_id_from_hwnd(hwnd)
[概要] プロセスIDの取得
[詳細説明] 指定されたウィンドウハンドルからプロセスIDを取得する
[引数] hwnd: CFemtet.hWndプロパティをセット
[引数の型] hwnd: int
[戻り値] プロセスID(取得できない場合は0を戻す)
[戻り値の型] int
[使用例]
import sys from femtetutils import util, const from win32com.client import Dispatch, constants util.auto_execute_femtet() Femtet = Dispatch(const.CFemtet) if Femtet.OpenNewProject() == False:
print(Femtet.LastErrorMsg)
sys.exit() procid = util.get_process_id_from_hwnd(Femtet.hWnd) print(procid)
is_femtet_active()
[概要] Femtet.exeが起動中か
[詳細説明] Femtet.exeが現在起動しているかを判定する
[戻り値] True:起動中, False:起動していない
[戻り値の型] bool
[使用例]
from femtetutils import util, const print(util.is_femtet_active())


