inkex.command module#

This API provides methods for calling Inkscape to execute a given Inkscape command. This may be needed for various compiling options (e.g., png), running other extensions or performing other options only available via the shell API.

Best practice is to avoid using this API except when absolutely necessary, since it is resource-intensive to invoke a new Inkscape instance.

However, in any circumstance when it is necessary to call Inkscape, it is strongly recommended that you do so through this API, rather than calling it yourself, to take advantage of the security settings and testing functions.

exception inkex.command.CommandNotFound[source]#

Bases: OSError

Command is not found

exception inkex.command.ProgramRunError(program, returncode=None, stderr=None, stdout=None, args=None)[source]#

Bases: ValueError

A specialized ValueError that is raised when a call to an external command fails. It stores additional information about a failed call to an external program.

If only the program parameter is given, it is interpreted as the error message. Otherwise, the error message is compiled from all constructor parameters.

program: str#

The absolute path to the called executable

returncode: int#

Return code of the program call

stderr: str#

stderr stream output of the call

stdout: str#

stdout stream output of the call

arguments: List#

Arguments of the call

inkex.command.which(program)[source]#

Attempt different methods of trying to find if the program exists.

inkex.command.write_svg(svg, *filename)[source]#

Writes an svg to the given filename

inkex.command.to_arg(arg, oldie=False)[source]#

Convert a python argument to a command line argument

inkex.command.to_args(prog, *positionals, **arguments)[source]#

Compile arguments and keyword arguments into a list of strings which Popen will understand.

Parameters

prog – Program executable prepended to the output.

Arguments
  • (str) – String added as given

  • (tuple) – Ordered version of Keyword Arguments, see below

Keyword Arguments
  • name (str) – Becomes --name="val"

  • name (bool) – Becomes --name

  • name (list) – Becomes --name="val1"

  • n (str) – Becomes -n=val

  • n (bool) – Becomes -n

Returns

Returns a list of compiled arguments ready for Popen.

Return type

list[str]

inkex.command.to_args_sorted(prog, *positionals, **arguments)[source]#

same as to_args(), but keyword arguments are sorted beforehand

New in version 1.2.

inkex.command._call(program, *args, **kwargs)[source]#
inkex.command.call(program, *args, **kwargs)[source]#

Generic caller to open any program and return its stdout:

stdout = call('executable', arg1, arg2, dash_dash_arg='foo', d=True, ...)

Will raise ProgramRunError if return code is not 0.

Keyword Arguments
  • return_binary

    Should stdout return raw bytes (default: False)

    New in version 1.1.

  • stdin – The string or bytes containing the stdin (default: None)

All other arguments converted using to_args() function.

inkex.command.inkscape(svg_file, *args, **kwargs)[source]#

Call Inkscape with the given svg_file and the given arguments, see call().

Returns the stdout of the call.

Changed in version 1.3: If the “actions” kwargs parameter is passed, it is checked whether the length of the action string might lead to issues with the Windows CLI call character limit. In this case, Inkscape is called in –shell mode and the actions are fed in via stdin. This avoids violating the character limit for command line arguments on Windows, which results in errors like this: [WinError 206] The filename or extension is too long. This workaround is also possible when calling Inkscape with long arguments to –export-id and –query-id, by converting the call to the appropriate action sequence. The stdout is cleaned to resemble non-interactive mode.

inkex.command.inkscape_command(svg, select=None, actions=None, *args, **kwargs)[source]#

Executes Inkscape batch actions with the given <svg> input and returns a new <svg>.

inkscape_command(‘<svg…>’, [select=…], [actions=…], […])

inkex.command.take_snapshot(svg, dirname, name='snapshot', ext='png', dpi=96, **kwargs)[source]#

Take a snapshot of the given svg file.

Resulting filename is yielded back, after generator finishes, the file is deleted so you must deal with the file inside the for loop.

inkex.command.is_inkscape_available()[source]#

Return true if the Inkscape executable is available.