使用 Python 获取 Visio Shape.BoundingBox 方法

Get Visio Shape.BoundingBox method with Python

我正在使用 Python 和 win32com.client 来获取 Microsoft Visio 绘图的页面名称和形状描述。下面的 Python 代码用于获取形状索引、形状名称和形状文本。获取形状边界框的命令失败,索引无效。

import sys, win32com.client
import copy

def main ():

    try:
        visio = win32com.client.Dispatch("Visio.Application")
        visio.Visible = 0

        dwg = visio.Documents.Open("C:\Users\John\Drawing1.vsdx")

        # Used by Visio Shape.BoundingBox method
        intFlags = 0 
        visBBoxUprightWH = 0x1

        try:

            vsoShapes = dwg.Pages.Item(1).Shapes # Get shapes for Visio Page-1

            for s in range (len (vsoShapes)):  

                # This line works
                print "Index = %s, Shape = %s, Text = %s" % (vsoShapes[s].Index, vsoShapes[s].Name, vsoShapes[s].Text)

                dblLeft =0.0
                dblBottom =0.0 
                dblRight = 0.0
                dblTop = 0.0

                # ====== This line will fail with invalid syntax =======
                vsoShapes.Item(s).BoundingBox intFlags + visBBoxUprightWH, dblLeft, dblBottom, dblRight, dblTop

        except Exception, e:
              print "Error", e
        dwg.Close()
        visio.Quit()

    except Exception, e:
        print "Error opening visio file",e
        visio.Quit()

main()

如何从 Python

中调用此 Visio 命令
vsoShapes.Item(s).BoundingBox intFlags + visBBoxUprightWH, dblLeft, dblBottom, dblRight, dblTop

Shape.BoundingBox 命令的 Microsoft 文档位于此处:https://msdn.microsoft.com/en-us/library/office/ff766755.aspx

这些dblLeft, dblBottom, dblRight, dblTop是类型化的输出参数。分配 0.0 无助于使它们成为这样。试试这个:

dblLeft, dblBottom, dblRight, dblTop = vsoShapes[s].BoundingBox(intFlags+visBBoxUprightWH)

查看类似问题: Python win32 com : how to handle 'out' parameter?