如何在Windows PowerShell中正确编写Inkscape Shell批量SVG处理脚本?
Optimizing Inkscape Batch Processing in PowerShell Using Shell Mode (Inkscape 0.92.3)
Your original script is slow because it launches a new Inkscape process for every SVG file—this adds significant overhead. The key to fixing this is to run one instance of Inkscape in shell mode and send all your processing commands to it, instead of spawning a new process each time.
Correct PowerShell Script Using Inkscape Shell Mode
Here's the optimized script tailored for Inkscape 0.92.3, which maintains a single Inkscape shell session and processes all files sequentially:
# Get all SVG files in the target directory $files = Get-ChildItem "C:\temp\SVG Charts" -Filter *.svg # Build a list of Inkscape shell commands for each file $inkscapeCommands = foreach ($f in $files) { # Safely construct the output path (avoids manual string concatenation errors) $outFile = Join-Path -Path $f.DirectoryName -ChildPath "Adjusted_$($f.BaseName).svg" # Inkscape 0.92.3 shell command syntax: wrap paths with quotes to handle spaces "file-open:`"$($f.FullName)`"; export-area-drawing; export-plain-svg:`"$outFile`";" } # Add the quit command to exit Inkscape shell when done $inkscapeCommands += "quit;" # Pipe all commands to the single Inkscape shell instance $inkscapeCommands | inkscape --shell
Why Your Previous Attempt Failed
Let's break down the issues in your shell mode script:
- Repeatedly launching Inkscape shell: Putting
inkscape --shellinside the loop spawns a new Inkscape process for every file—this defeats the purpose of shell mode and causes errors from unclosed sessions. - Incorrect command syntax: Inkscape 0.92.3 doesn't use the
export-filename/export-doworkflow (that's a newer 1.0+ feature). Instead,export-plain-svgaccepts the output filename directly as an argument. - Unquoted paths: If your file paths contain spaces (like your directory "SVG Charts"), Inkscape will misparse them unless wrapped in quotes.
Key Notes for Inkscape 0.92.3 Shell Mode
- Single session only: Always launch
inkscape --shellonce, then send all commands to it via stdin (the pipe in PowerShell). - Command formatting: Separate commands with semicolons, and wrap paths with double quotes to handle spaces.
- Order matters: For each file, you must:
- Open the file with
file-open:"path" - Adjust the area with
export-area-drawing - Export with
export-plain-svg:"output-path"
- Open the file with
This approach will drastically speed up your batch processing since you're only initializing Inkscape once, not per file.
内容的提问来源于stack exchange,提问作者Tumbledown
相关产品推荐
相关产品推荐

