Search code examples
pythonpywin32visio

Fill a visio element with color using python


I’m using this code to draw myself a server in visio:

import win32com.client as w32
visio = w32.Dispatch("visio.Application")
visio.Visible = 1
doc = visio.Documents.Add("Detailed Network Diagram.vst")
page = doc.Pages.Item(1)
page.name = "My drawing"
stn2 = visio.Documents("Servers.vss")
server = stn2.Masters("Server")
serv = page.Drop(server, 0, 0)
for ssh in serv.shapes:
    ssh.Cells( 'Fillforegnd' ).FormulaForceU = 'RGB(255,0,0)'

And my problem is when I’m trying to fill the object with a color (instead of the regular server color) it doesn’t work. Nothing really worked. I’m using python 3.8.


Solution

  • Try this code please

    import win32com.client as w32
    visio = w32.Dispatch("visio.Application")
    visio.Visible = 1
    doc = visio.activedocument
    page = doc.pages(1)
    page.name = "Mydrawing"
    stn2 = visio.Documents(2)
    server = stn2.Masters(2)
    serv = page.Drop(server, 0, 0)
    #iterate all sub-shapes into Serv-shape
    for ssh in serv.shapes:
        ssh.Cells( 'Fillforegnd' ).FormulaForceU = 'RGB(255,255,0)'
    

    If you dont need iterate all sub-shapes, you can change only same of them

    #iterate 2nd, 3rd and 4rd sub-shapes into Serv-shape #     
    for i in range (2,5):     
        ssh = serv.shapes(i)
        # if you need get solid color for sub-shapes uncomment next line    
        # ssh.Cells('FillPattern').FormulaForceU = '1'    
          ssh.Cells('Fillforegnd').FormulaU = 'RGB(255,255,0)'    
    

    Code in my Jupyterlab notebook change only 3 sub-shapes, which i select and delete for demonstrate difference…
    enter image description here


    PS The user's problem was not in the code, but in the Visio sub-shapes, which did not want to inherit the color of the main shape. Because these sub-shapes had formulas that used functions like THEMEGUARD and similar to it in their cells.
    I modified the shape from the built-in set of elements and the problem was solved…

    PPS Solved! To remove the dependency on those sub-shapes, you need to change their Fillstyle to Normal. Just add new line of code ssh.FillStyle = 'Normal'.
    Look at code ↓

    import win32com.client as w32
    visio = w32.Dispatch("visio.Application")
    visio.Visible = 1
    # create document based on Detailed Network Diagram template (use full path)
    doc = visio.Documents.Add ("C:\Program Files\Microsoft Office\root\Office16\visio content\1033\dtlnet_m.vstx")
    # use one of docked stencils 
    stn2 = visio.Documents("PERIPH_M.vssx")
    # define 'Server' master-shape
    server = stn2.Masters("Server")
    # define page
    page = doc.Pages.Item(1)
    # rename page
    page.name = "My drawing"
    # drop master-shape on page, define 'Server' instance
    serv = page.Drop(server, 0, 0)
    # iterate sub-shapes (side edges) 
    for i in range (2,6):
        # define one od side edges from 'Server'
        ssh = serv.shapes(i) 
        # Change Fill Style to 'Normal'
        ssh.FillStyle = 'Normal'
        # fix FillForegnd cell for side edge
        ssh.Cells( 'Fillforegnd' ).FormulaForceU = 'Guard(Sheet.' + str(serv.id) +  '!FillForegnd)'
        # fix FillBkgnd cell for side edge
        ssh.Cells( 'FillBkgnd' ).FormulaForceU = 'Guard(Sheet.' + str(serv.id) +  '!FillBkgnd)'
        # instead formula 'Guard(x)' rewrite formula 'Guard(1)'    
        ssh.Cells( 'FillPattern' ).FormulaForceU = 'Guard(1)'
    
    # fill main shape in 'Server' master-shape
    serv.Cells("FillForegnd").FormulaForceU = '5'