DEV Community

codemee
codemee

Posted on

用 Inkscape 批次幫 SVG 檔文字轉外框

我的工作會遇到需要把 SVG 檔內含的文字轉外框,這可以透過開放原始碼的 Inkscape 處理,而且 Inkscape 還提供 CLI 介面,可以像是透過 Selenium 或是 Playwright 操控瀏覽器那樣自動化。於是我就請 Grok 幫我設計腳本,讓我可以批次處理一堆 SVG 檔轉外框在存檔的功能,以下是透過指令把 SVG 轉外框的批次檔:

@echo off
REM batch_text_to_path.bat
for %%f in (*.svg) do (
    echo Processing %%f...
    "C:\Program Files\Inkscape\bin\inkscape.exe" --actions="select-all;org.inkscape.text.convert_to_path;export-filename:%%~nf_converted.svg;export-do" "%%f"
)
Enter fullscreen mode Exit fullscreen mode

它會把目前資料夾下的所有 SVG 檔用 Inkscape 一一開啟,開啟後:

  1. 選取所有物件
  2. 文字轉外框
  3. 以原檔名加上 "_converted" 字尾存檔

也有 bash 版:

#!/bin/bash
# batch_text_to_path.sh
for file in *.svg; do
    echo "Processing $file..."
    inkscape --actions="select-all;org.inkscape.text.convert_to_path;export-filename:${file%.svg}_converted.svg;export-do" "$file"
done
Enter fullscreen mode Exit fullscreen mode

或是 PowerShell 版:

# batch_text_to_path.ps1
$inkscapePath = "C:\Program Files\Inkscape\bin\inkscape.exe" # 根據你的 Inkscape 安裝路徑調整
$svgFiles = Get-ChildItem -Filter *.svg

foreach ($file in $svgFiles) {
    Write-Host "Processing $($file.Name)..."
    & $inkscapePath --actions="select-all;org.inkscape.text.convert_to_path;export-filename:$($file.BaseName)_converted.svg;export-do" $file.FullName
    Write-Host "Saved as $($file.BaseName)_converted.svg"
}
Enter fullscreen mode Exit fullscreen mode

如果想知道有哪些動作可以透過 CLI 完成,可以執行 --action-list

c:\Program` Files\Inkscape\bin\inkscape --action-list
Enter fullscreen mode Exit fullscreen mode

Top comments (0)