使用 python 按顺序从上到下读取 VISIO 文件的内容

Reading the content of a VISIO file from top to bottom in a sequence using python

我试图在 excel sheet 中写下形状的名称和相应的文本,即从上到下。我编写的程序随机读取覆盖所有块的任何位置的数据。

我想让我的程序按照图表的流程从上到下依次读取文件。

这是我正在使用的程序

pages = application.ActiveDocument.Pages
i=1
for page in pages:
    print('SheetName:' + str(page))
    i=i+1
    sheet1.write(i,0,('SheetName:' + str(page)))
    for shape in page.Shapes:
        print (shape.Name + " '" + shape.Text)
        sheet1.write(i,1,shape.Name)
        sheet1.write(i,2,shape.Text)
        i=i+1
wb.save('example1.xls')'

你能帮我 this.Thank 你吗

不幸的是,Visio 中没有像 page.GetShapesAccordingToTheGraph 这样的方法,您需要自己做。作为第一步,您可以找到所有形状互连(构建 "graph"),作为第二步,按对您有意义的顺序遍历该图。

  1. 构建图表。您可以使用 Visio shape.ConnectedShapesConnects 来查找连接的形状。您可以参考这篇很棒的文章来开始寻找联系:https://blog.bvisual.net/2016/08/09/understanding-visio-connections/

  2. 遍历。最简单的方法是使用 Bread First Search,例如 Depth First Search。这完全取决于你的形状是如何连接的(例如,是否有 loops/cycles,以及你想如何处理替代路径)。

在没有环路和备用路径的简单情况下,(2) 实际上只是根据它们的(单个)传出连接对形状进行迭代。 首先找到没有传入边的形状,然后从该形状开始,使用传出边获取与其相连的形状,然后重复该相连形状,直到没有更多。