Is it possible to execute/call an external application in Windows or a batch file (bat) using the QGIS Graphic Modeller? I am using QGIS version 3.28.7
1 Answer
You could create a processing script that calls the bat/sh/exe using the subprocess
python module.
E.g. a simple processing script that takes one input, a CSV file, passes it to an external batch file and captures the output of the batch file (the number of lines in the input CSV) and returns that:
from qgis.processing import alg
import subprocess
@alg(name="shellscript", label=alg.tr("shellscript"), group='examplescripts', group_label='Example scripts')
@alg.input(type=alg.FILE, name="CSV_INPUT", label="CSV File")
@alg.output(type=alg.NUMBER, name='OUTPUTFEATURES', label='Number of features processed')
def shellscript(instance, parameters, context, feedback, inputs):
"""Description goes here. (Don't delete this! Removing this doc string will cause errors.)"""
output = subprocess.check_output(["/path/to/test.bat", parameters["CSV_INPUT"]])
return {'OUTPUTFEATURES': int(output)}
This processing script can then be added to a model:
-
Thank you @user2856 exactly what I needed. Never tried it but time to learn something new.– GforGISJun 9 at 2:50
subprocess.run
: docs.python.org/3.6/library/subprocess.html#subprocess.run