使用minidom/etree/lxml解析python svg

2024-09-30 06:15:03 发布

您现在位置:Python中文网/ 问答频道 /正文

使用python,我试图用minidom/etree/lxml解析svg(xml格式) 我有以下svg文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- Created with Inkscape (http://www.inkscape.org/) --> <svg xmlns:shaper="http://www.shapertools.com/namespaces/shaper" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" width="5319.4434mm" height="6025.5mm" viewBox="0 0 5319.4434 6025.5" version="1.1" id="svg1217" inkscape:version="0.92.4 (5da689c313, 2019-01-14)" sodipodi:docname="test.svg"> <defs id="defs1211" /> <sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="0.0875" inkscape:cx="2965.7333" inkscape:cy="12836.281" inkscape:document-units="mm" inkscape:current-layer="layer2" showgrid="false" fit-margin-top="0" fit-margin-left="0" fit-margin-right="0" fit-margin-bottom="0" inkscape:window-width="1920" inkscape:window-height="1017" inkscape:window-x="-8" inkscape:window-y="-8" inkscape:window-maximized="1" /> <metadata id="metadata1214"> <rdf:RDF> <cc:Work rdf:about=""> <dc:format>image/svg+xml</dc:format> <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> <dc:title></dc:title> </cc:Work> </rdf:RDF> </metadata> <g inkscape:groupmode="layer" id="layer2" inkscape:label="Second_Layer" style="display:inline"> <rect style="fill:none;fill-opacity:1;stroke:#000000;stroke-width:10;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;opacity:1" id="rect2567" width="1233.7142" height="1644.9524" x="459.61902" y="268.1666" /> </g> <g inkscape:label="First_Layer" inkscape:groupmode="layer" id="layer1" transform="translate(2594.7098,2684.756)" style="display:none"> <g transform="matrix(10,0,0,10,-2594.7098,-2684.756)" id="Body_Körper116"> <path style="vector-effect:non-scaling-stroke;fill:#000000" inkscape:connector-curvature="0" d="M 241.25,-479.9 V -500 h 17.5 v 20.1 z" shaper:cutDepth="0.0001105" shaper:pathType="exterior" transform="matrix(1,0,0,-1,-42.65,18.6)" id="path2" /> </g> <g transform="matrix(10,0,0,10,-2594.7098,-2684.756)" id="Body_Körper114"> <path style="vector-effect:non-scaling-stroke;fill:#000000" inkscape:connector-curvature="0" d="M 141.95,-480.1 V -500 h 16.1 v 19.9 z" shaper:cutDepth="0.0001103" shaper:pathType="exterior" transform="matrix(1,0,0,-1,-42.65,18.6)" id="path5" /> </g>

其中包括2层(第1层和第2层),每层中有一些车身并分组。 我想做的是在这些物体上执行操作(例如变换)来移动它们等等

我尝试了minidom和etree,但在我的例子中,我无法清楚地了解这些树是如何构建和访问的。 因此,我有几个问题,希望有更多经验的人能帮助我快速步入正轨:)

  1. 使用以下任一选项时:

    lxml=ET.parse('test.svg') svgdoc=lxml.getroot()

svgdoc = xml.dom.minidom.parse('test.svg')

我设法把文件读入树中。有人能告诉我如何访问上述案例中的每一棵树、子树、属性等吗?希望类似于“svgdoc.parent[1].child[1].attribute的内容通过树及其节点,但我遇到了很多错误

  1. 例如,对于具有“id=”Body_örper114”的Body,我必须如何访问“transform=”矩阵(10,0,0,10,-2594.7098,-2684.756)”以及“shaper:cutDepth=“0.0001105”如何嵌套在Body_örper114的内部

  2. 我可以通过电脑阅读 path_d_strings = [path.getAttribute('d') for path in svgdoc.getElementsByTagName('path')] 但是,如果我想根据在路径中找到的某些条件更改嵌套在路径中的变换而不是(而是直接在上面),那么如果我使用getElementsByTagName方法,我最终会得到更多的“变换”-元素,例如“shaper:cutDepth”,因为“变换”不仅在路径中出现为“shaper:cutDepth…”但也在上面的正文描述中,因此我不能简单地按索引浏览列表并应用更改

  3. 我还发现奇怪的是,我将svgdoc中的元素读取到其他一些列表中,在这个列表中进行更改,然后在写入更改的同时写入xml文件

path = svgdoc.getElementsByTagName("path") firstchild = path[0] firstchild.attributes["d"].value = "test" f = open('test2.svg', 'w') f.write(doc.toxml()) doc.unlink()

我希望在保存之前必须将getElements提取的列表传输回svgdoc。这个结构是如何工作的

  1. 使用Inkscape,我可以删除所有变换部分,并将所有内容置于绝对坐标。当我需要变换身体并用脚本移动它们时,我需要插入一个新的“节点”?!?在路径下(如“样式”或“inkscape”),并将其保存到xml文件中。我将如何处理这个问题

很抱歉发了这么长的帖子,但这真的让我很烦,我试着去理解到底发生了什么。 我阅读了教程、文档和示例,但没有真正说明如何控制这种结构。此外,我是python新手,还需要了解语法(不幸的是,我没有太多时间,因为我有两个小孩)

谢谢你的帮助,非常感谢! 干杯

托比


Tags: pathsvgorgidhttpstrokewwwtransform
1条回答
网友
1楼 · 发布于 2024-09-30 06:15:03

你的帖子太长了,我听不懂。这里有一个例子。我不知道它是否能满足你的需要。如果您有任何问题,请留言

from simplified_scrapy import SimplifiedDoc,req,utils

html = utils.getFileContent('test.svg')
doc = SimplifiedDoc(html)
path = doc.getElement('path') # Select element
path.setAttr('d','test') # Edit attribute
while True:
  path = doc.getElement('path',start=path._end)
  if not path: break
  path.setAttr('d','test2')
utils.saveFile('test2.svg', doc.html)

相关问题 更多 >

    热门问题