John Bessire
John Bessire

Reputation: 571

Get Visio Shape.BoundingBox method with Python

I am using Python with the win32com.client to get the page names and shapes description for a Microsoft Visio drawing. The Python code below works for getting the shape index, shape name and shape text. The command to get the shape bounding box fails with an invalid index.

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()

How do you call this Visio command from Python

vsoShapes.Item(s).BoundingBox intFlags + visBBoxUprightWH, dblLeft, dblBottom, dblRight, dblTop

The Microsoft documention for the Shape.BoundingBox command is located here:https://msdn.microsoft.com/en-us/library/office/ff766755.aspx

Upvotes: 2

Views: 1554

Answers (1)

Nikolay
Nikolay

Reputation: 12245

These dblLeft, dblBottom, dblRight, dblTop are typed output parameters. Assigning 0.0 will not help to make them such. Try this instead:

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

Check out a similar question: Python win32 com : how to handle 'out' parameter?

Upvotes: 3

Related Questions