Chapter-3 Visio automation with Python-Code

 

The Code Explanation

In previous chapters we saw some codes which was able to draw some basic diagrams on the screen but i am sure when you guys implemented the same on your machine it shouldn't have worked.

So Let's not waste time and start our discussion.

Code Snippet-1

from hld_exp import Visio

The above code must have been  giving you errors since Python on your machine didn't recognize a library name hld_exp.

To be honest there is no such library as of now anywhere. I am working on a project with few motivated experienced engineers to develop such a library to automate MS Visio with Python and 'hld_exp' is the result of our work and we are going to talk about this library and it's functions. Which we can use to cater the requirement of custom Visio design.

The hld_exp is based on win32com library to interact with COM object model and we will discuss about this in detail in our subsequent chapters.

This is a code example taken from hld_exp and i would ask guys to go through these code once before we dive deep into discussing this further. 

from ast import Raise
import win32com.client
import os

class Visio:
    """ This class contains functions for visio drawings"""
    def __init__(self):        
        self.prog = None
        self.doc = None
        self.stencil = None
        self.mstobj = None
        self.page = None
        self.drawobj = None
        self.active = None
        self.shape = None
        self.cells = None
        self.connector = None
        self.connectorobj = None

    def connect(self):        
        """This function will return connector tool object"""        
        if self.prog is not None:
            self.connector = self.prog.Application.ConnectorToolDataObject
        else:            
            raise ValueError("Please invoke open function to open Visio")        
        return self.connector
               
    def open(self,app):
        """ This function will return Visio application object"""
        app = app + ".Application"
        print(app)
        self.prog = win32com.client.Dispatch(app)
        self.prog.Visible = 1
        print("Open Called")
        return self.prog
   
    def documents(self,doc):
        """"This function will return a Visio Document object"""
        self.doc = self.prog.Documents.Add(doc)
        return self.doc
   
    def selectpage(self,num):
        """This function will return a selected page object"""
        if self.doc is not None:
            self.page = self.doc.Pages.Item(num)
        else:
            print("Please add a document first")
        return self.page        

    def stencils(self,stn):
        """This function will return a stencil object"""
        cwd = os.getcwd()
        path = os.path.join(cwd,stn)
        self.stencil = self.prog.Documents.Open(path)
   
    def mastobj(self,obj):
        """This function will return a master object obtained from stencil object"""
        if self.stencil is not None:
            self.mstobj = self.stencil.Masters(obj)            
        else:
            print("Please invoke stencils function to open a .vssx template first")
        return self.mstobj
   
    def drawobject(self,obj,x,y,name = None):
        """ This function will draw object on page"""
        if self.mstobj is not None:
          self.drawobj =  self.page.Drop(obj,x,y)
          if name is not None:
            self.drawobj.Text = name
          else:
            pass
        else:
            print("No object is available")
        return self.drawobj
   
    def contool(self):
        """"This function will drop Connector object on page"""
        if self.connector is not None and self.page is not None:
            self.connectorobj = self.page.Drop(self.connector,0,0)
        else:
            print("No object to drop on page")
        return self.connectorobj
   
    def activedocument(self):
        """This function will identify Active document object"""
        if self.doc is not None:
            self.active = self.prog.ActiveDocument
        else:
            print("No active Document")
        return self.active  

    def shapeprop(self,type = None, name = None, ip = None):
        """This function will set Master object shape sheet custom property type value"""
        if self.drawobj is not None:
            self.drawobj.Cells('Prop._VisDM_Device_Type').Formula = f"\"{type}\""
        else:
            print("No object has been found")

    def quit(self):
        """This function will quit the application"""
        self.prog.Quit()
   
    def close(self,stn):
        """This function will close the Stencil document"""
        self.stencil.Close()        
        print("Stencil Closed")
        return self.stencil

Hope you guys have gone through code were able to grasp what the above code is about. I will discuss this in upcoming chapters. Please stay tuned for updates.

Comments

Popular posts from this blog

Chapter-2- Visio automation with Python-Code

Chapter-1 Visio automation with Python