Skip to content

Instantly share code, notes, and snippets.

@rdapaz
Last active May 22, 2020 05:18
Show Gist options
  • Save rdapaz/5a84ff0182da1c8fa321cfa7673c7896 to your computer and use it in GitHub Desktop.
Save rdapaz/5a84ff0182da1c8fa321cfa7673c7896 to your computer and use it in GitHub Desktop.
Python script to do a visio find and replace
import re
import win32com.client as c
class VisioDrawing:
def __init__(self, path, pageName):
self.path = path
self.vsdApp = c.gencache.EnsureDispatch('Visio.Application')
self.vsdApp.Visible = True
self.vsd = self.vsdApp.Documents.Open(path)
self.pge = self.vsd.Pages(pageName)
self.from_to = {
"one": "1",
"two": "2",
"three": "3",
}
def ReplaceTextEntries(self):
for findText, replacementText in self.from_to.items():
for shp in self.pge.Shapes:
if shp.Text and shp.Text == findText:
replacementText = self.from_to[findText]
shp.Text = replacementText
def ListIncorrectNames(self):
rex = re.compile(r'VOKH\w+[0-9\-]+')
for shp in self.pge.Shapes:
if rex.search(shp.Text):
print(shp.Text)
if __name__ == '__main__':
path = r'<CHANGEME.vsdx>'
pageName = "Page-1"
vsd = VisioDrawing(path, pageName)
vsd.ListIncorrectNames()
vsd.ReplaceTextEntries()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment