Inkscape cli: select all text objects filled with a specific color and align all to bottom

142 Views Asked by At

I have a svg file that contains many text objects filled with the color 0070c0. It contains also other objects.

I would like, via CLI, to select only those text objects with that fill, and then vertically align them all to bottom of the page.

I have tried to select those using this command, but I cannot select using the fill=#0070c0 parameter

inkscape --actions="select-by-selector:text;select-invert;delete;export-plain-svg;export-filename:out.svg;export-do" input.svg
1

There are 1 best solutions below

1
s.ouchene On BEST ANSWER

Currently, in Inkscape, select-by-selector has some limitations (see this for details). however, you can use lxml module in Python or any other tool to get the IDs of the text elements with a specific fill color. Then we can use Inkscape actions to align the text elements.

  1. The following Python script get_ids.py extracts the IDs of text elements with a specific fill color:

get_ids.py:

from lxml import etree

# Change the input filename
tree = etree.parse("input.svg")
namespaces = {'svg': 'http://www.w3.org/2000/svg'}

# Change the fill color
elements = tree.xpath("//svg:text[contains(@style, 'fill:#0000ff')]/@id", namespaces=namespaces)

print(','.join(elements))

Make sure you have lxml installed with:

python3 -m pip install lxml
  1. Use inkscape CLI to align the text elements with the bottom of the page:

In Unix-like shells such as bash:

inkscape --actions="select-by-id:$(python3 get_ids.py); object-align: bottom  page;export-do" input.svg

On Windows using Powershell (I haven't tested it but it should work):

$id = python get_ids.py
inkscape --actions="select-by-id:$id;object-align:bottom page;export-do" input.svg

Result:

Before:

Before alignment

After:

After alignment