我有一个 svg 文件,其中包含许多填充颜色
0070c0
的文本对象。它还包含其他对象。
我想通过 CLI 仅选择那些具有该填充的文本对象,然后将它们全部对齐到底部。
我尝试使用此命令选择那些,但无法使用
fill=#0070c0
参数进行选择
inkscape --actions="select-by-selector:text;select-invert;delete;export-plain-svg;export-filename:out.svg;export-do" input.svg
目前,在 Inkscape 中,
select-by-selector
有一些限制(请参阅此处了解详细信息)。但是,您可以使用 Python 中的 lxml
模块或任何其他工具来获取具有特定填充颜色的文本元素的 ID。然后我们可以使用 Inkscape 操作来对齐文本元素。
get_ids.py
提取具有特定填充颜色的文本元素的ID: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))
确保您已安装 lxml:
python3 -m pip install lxml
在 bash 等类 Unix shell 中:
inkscape --actions="select-by-id:$(python3 get_ids.py); object-align: bottom page;export-do" input.svg
在 Windows 上使用 Powershell(我还没有测试过,但它应该可以工作):
$id = python get_ids.py
inkscape --actions="select-by-id:$id;object-align:bottom page;export-do" input.svg
之前:
之后: