Inkscape Export Multi-Page script bash

Enjoy !

Requirements :

  • Inkscape > 1.1
  • xmlstarlet
#!/bin/bash

if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <input_file.svg>"
    exit 1
fi

INPUT_FILE="$1"
BASE_NAME="$(basename "$INPUT_FILE" .svg)"

echo "Processing SVG file: $INPUT_FILE"

# Extract page attributes using xmlstarlet
mapfile -t PAGES < <(xmlstarlet sel -N i="http://www.inkscape.org/namespaces/inkscape" -t \
    -m "//i:page" -v "concat(@id, ';', @i:label, ';', @x, ';', @y, ';', @width, ';', @height)" -n "$INPUT_FILE")

if [ "${#PAGES[@]}" -eq 0 ]; then
    echo "No pages found in the SVG file."
    exit 1
fi

# Loop through extracted pages and export them
for PAGE in "${PAGES[@]}"; do
    IFS=';' read -r PAGE_ID PAGE_LABEL X Y WIDTH HEIGHT <<< "$PAGE"
    OUTPUT_FILE="${BASE_NAME}_${PAGE_LABEL}.png"

    echo "Exporting page '$PAGE_LABEL' (ID: $PAGE_ID) to '$OUTPUT_FILE'"

    # Calculate export area
    X=${X:-0}
    Y=${Y:-0}
    WIDTH=${WIDTH:-0}
    HEIGHT=${HEIGHT:-0}

    # Compute export area using bc (handles floating-point arithmetic)
    X2=$(echo "$X + $WIDTH" | bc)
    Y2=$(echo "$Y + $HEIGHT" | bc)

    # Export using the calculated area
    inkscape "$INPUT_FILE" --export-area="${X}:${Y}:${X2}:${Y2}" --export-filename="$OUTPUT_FILE"

done

echo "Export completed."

You have a project ?

contact me ! => paul.millet@lameduse.fr

Socials