Inkscape批量将inkscape:label(标签)复制给id

最近在做毕设,遇到一个需要重复复制粘贴的东西,顺手写了一个python脚本。目标是,将Inkscape绘制SVG文件的<g><path><rect><text> 标签的属性inkscape:label批量复制给id

github地址https://github.com/Robert30-xl/SVG-setAttribute-for-Inkscape

SVG-setAttribute-for-Inkscape是一个python脚本。使用Inkscape绘制SVG文件,在Inkscape的”图层和对象“可以很方便地为属性inkscape:label赋值,但是id属性需要再次复制粘贴,这个无聊并且重复的工作交给python吧!

import xml.dom.minidom 

dom = xml.dom.minidom.parse('你的SVG文件.svg')  #打开svg文档(这里将SVG和脚本放到一个目录)
root = dom.documentElement      #得到文档元素对象
gList = root.getElementsByTagName('g')    #得到所有g标签
pathList = root.getElementsByTagName('path')    #得到所有path标签
rectList = root.getElementsByTagName('rect')    #得到所有rect标签
textList = root.getElementsByTagName('text')    #得到所有text标签

# 设置g
for index in range(len(gList)):
    label = gList[index].getAttribute('inkscape:label')
    if gList[index].hasAttribute('inkscape:label'):
        # id = gList[index].getAttribute('id')
        gList[index].setAttribute('id',label[1:])

# 设置path
for index in range(len(pathList)):
    label = pathList[index].getAttribute('inkscape:label')
    if pathList[index].hasAttribute('inkscape:label'):
        # id = pathList[index].getAttribute('id')
        pathList[index].setAttribute('id',label[1:])

# 设置rect
for index in range(len(rectList)):
    label = rectList[index].getAttribute('inkscape:label')
    if rectList[index].hasAttribute('inkscape:label'):
        # id = rectList[index].getAttribute('id')
        rectList[index].setAttribute('id',label[1:])

# 设置text
for index in range(len(textList)):
    label = textList[index].getAttribute('inkscape:label')
    if textList[index].hasAttribute('inkscape:label'):
        # id = textList[index].getAttribute('id')
        textList[index].setAttribute('id',label[1:])

'''
将文档进行重写,注意文档中内容含有中文的情况
open()函数默认是文本模式打开,也就是ANSII编码
在简体中文系统下,ANSII编码代表 GB2312 编码
'''
with open('HPC发端模型.svg', 'w', encoding='utf-8') as f:
    # 缩进 - 换行 - 编码
    dom.writexml(f, addindent='', encoding='utf-8') 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值