Search code examples
pythonvisio

Get layer of shape in Visio in Python


I'm trying to select all shapes within an existing Visio-file that are i. e. in layer "help" to delete them. Is there a way to achive that using python package "vsdx"?


Solution

  • You can use CreateSelection method with parameter visSelTypeByLayer = 3.

    Right now I haven't Python environment there, my simple VBA code

    Sub SOF_77409035()
    Dim Sh As Shape
    Dim sl As Selection
    ActiveWindow.DeselectAll ' deselect all   
    Set sl = ActivePage.CreateSelection(3, 0, "help") ' create selection in memory    
    For Each Sh In sl ' iterate all shapes in selection    
    '    ActiveWindow.Select Sh, 2 ' select shape visSelect = 2    
         sh.delete    
    Next    
    End Sub   
    
    

    UPDATE This python-code can print a list of shapes belonging to a layer.
    But it cannot select these shapes.

    import win32com.client as w32 
    visio = w32.Dispatch("visio.Application") 
    visio.Visible = 1 
    win = visio.activewindow
    doc = visio.activedocument
    pag = doc.pages(1)
    sl = pag.createselection(3, 0,"help")
    for i in range (1, sl.count+1):
        sh = sl.item(i)
        sh.delete 
    

    PS vsdx python package have poor documentation, I suspect that's not possible.