我有一个用原始.dot格式制作的模板示意图,但现在我想使用python填充标签。
使用https://pypi.org/project/graphviz/库,我设法加载了.dot文件,但我不知道如何编辑它。是否可以将Source
对象转换为Graph
对象,或者使用Graph对象可用的方法?
正在尝试:
from graphviz import Source
src = Source('digraph "the holy hand grenade" { rankdir=LR; 1 -> 2 -> 3 -> lob }')
src.node("foo")
_ = src.render('test.gv', view=False)
Source.from_file('test.gv')
我得到错误AttributeError: 'Source' object has no attribute 'node'
发布于 2021-04-18 07:15:07
我确实解析了grapviz.source
,删除了不必要的字符,然后追加回来。这对我很有效。请注意,此假设第一行可能是包含注释。剩下的事情就是让它成为函数。
src = Source.from_file('Source.gv')
lst = str(src).splitlines()
HasComment = (lst[0].find('//') != -1)
IsDirectGraph = False
skipIndex = 0
if HasComment:
skipIndex = 1
if lst[skipIndex].find('graph {')!=-1 :
IsDirectGraph= False
else:
if lst[0].find('graph {')!=-1 :
IsDirectGraph= False
if IsDirectGraph:
g = Digraph()
else:
g = Graph()
g.body.clear()
s = str()
for i in range(len(lst)):
if( (i>skipIndex) and (i!=len(lst)-1) ):
if HasComment:
g.comment = lst[0].replace('//','')
g.body.append(lst[i])
print(g.source)
display(g)
发布于 2020-02-12 00:15:29
https://github.com/xflr6/graphviz/issues/76
回答了这个库不可能实现的问题,但是可以使用其他库。
https://stackoverflow.com/questions/60173042
复制相似问题