How to make text alignment of Visio shapes using Python(Win32 module) - python

I read the documentation of VBA and couldn't convert this to Python code.
I simply created shape and tried to make its align right.
If I delete alignment line of code, rest code works perfectly.
I don't know, I might be using wrong VBA objects and methods. I'm open to your advices. If you provide me your solution code, I would be very appreciated.
import win32com.client
app = win32com.client.Dispatch("Visio.Application")
app.Visible = True
my_shape = page.DrawRectangle(3, 3, 5, 5)) # Draw rectangle
my_shape.Text = "Hello world!" # Add text to rectangle
my_shape.LineStyle = "None" # Add some styling
my_shape.Characters.ParaProps['visHorzAlign'] = 'visAlignRight' #Tried alignment here
my_shape.SetCenter(4.4281, 3) # Change position of rectangle
Here is the error
Related documentations and forums that I found:
http://www.44342.com/visio-f963-t1206-p1.htm
https://learn.microsoft.com/en-us/office/vba/api/visio.selection.align

Try this code:
import win32com.client as win32
app = win32.Dispatch("Visio.Application")
app.Visible = True
page = app.Documents.AddEx("", 1, 0).Pages(1) # create new document and get 1 page
my_shape = page.DrawRectangle(3, 3, 5, 5) # Draw rectangle
my_shape.Text = "Hello world!" # Add text to rectangle
my_shape.LineStyle = "None" # Add some styling
my_shape.Cells("Para.HorzAlign").FormulaU = "2" # 2 - right alignment
# or use my_shape.CellsSRC(4, 0, 6).FormulaU = "2" #visSectionParagraph=4, visHorzAlign=6
my_shape.SetCenter(4.4281, 3) # Change position of rectangle

Related

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.
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…
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'

Save map with widget as video/html_video

I created a map with a widget. The widget looks as following:
time_slider = widgets.interactive(var_map_interactive, time_sel=widgets.IntSlider(description = "Time", min = 0, max = t_end, step = 5, readout_format = "d"), continous_update = True)
I now wanted to save this as a widget stream using the WidgetStream from ipywebrtc
view_stream = WidgetStream(widget=time_slider)
and then as a VideoRecorder
recorder = VideoRecorder(stream=view_stream)
However, eventhough the map with the widget works on its own in my Jupyter notebook, the "recorder" does not display anything.
Any ideas why this is happening?
Also, how would I be able to create a html_video out from the recording?
Thanks for your help!

Populate window in Maya with icons and dynamically adjust depending on window size

I want to create a window in maya, that gets populated with icons from a specific path. I know how to do that, but I also want the icons to adjust dynamically as I change the size of the window.
For example, let's say I have this:
enter image description here
and I want when I resize to get this:
enter image description here
here is a bit of the code I have :
import maya.cmds as cmds
import os
from os import listdir
def UI(*args):
if cmds.window("Test", exists = True):
cmds.deleteUI("Test")
testwindow = cmds.window("Test", t="Test Window", sizeable = 1)
cmds.scrollLayout('srcoll', p = "Test")
cmds.rowColumnLayout("ColLayout", p = "Test", nc = 3)#I imagine the "nc" command is probably useless here, I am just leaving it for testing purposes
cmds.showWindow("Test")
customPath = "C:\Users\$username$\Desktop"
customPathItems = listdir(customPath)
def popUI(*args):
for item in customPathItems:
if item.endswith("_icon.png"):
cmds.iconTextButton(l = item, p = "ColLayout", i = customPath + "/" + item, w = 128, h = 128)
def buildUI(*args):
UI()
popUI()
buildUI()
Any help would be appreciated
What you need is called a Flow layout, where the items inside the layout automatically adjust themselves when the widget is resized.
Here's an example from Qt's documentation that you can fully convert over to Python:
https://doc.qt.io/qt-4.8/qt-layouts-flowlayout-flowlayout-cpp.html
You can also google pyqt flow layout for ones already written in Python.

Creating a table in python docx and use small_caps (python-docx)

This is what I have tried:
from docx import Document
from docx.shared import Inches
from docx.shared import Pt
table = document.add_table(rows= 3, cols = 1)
row = table.rows[0]
runner = row.cells[0].paragraphs[0].add_run('Summary')
runner.bold = True
runner.small_caps = True
document.save('demo.docx')
I am trying to create a table and small caps the text but cannot get the syntax right
I have figured how to use small caps for text.
runner_font = runner.font
runner_font.size = Pt(14)
runner_font.small_caps = True
runner_font.bold = True
I believe bold and italic are properties that built in under both add_run and font. But for small_caps, all_caps, color can be called under font only.

How to add a link to a Visio document via Python

I googled for some code to create Visio documents via Python. I want to add shapes, and have hyperlinks. So that you can click on the shape, or preferably on the text inside the shape, and get to a URL.
import os
import win32com.client
from win32com.client import constants
appVisio = win32com.client.Dispatch("Visio.Application")
appVisio.Visible =1
doc = appVisio.Documents.Add("Basic Diagram.vst")
pagObj = doc.Pages.Item(1)
stnObj = appVisio.Documents("Basic Shapes.vss")
mastObj = stnObj.Masters("Rectangle")
shpObj1 = pagObj.Drop(mastObj, 4.25, 5.5)
shpObj1.Text = "This is some text."
shpObj2 = pagObj.Drop(mastObj, 2, 2)
shpObj2.Text = """This is some more text. {\field{\*\fldinst HYPERLINK "http://www.google.com/"}{\fldrslt http://www.google.com}}"""
connectorMaster = appVisio.Application.ConnectorToolDataObject
connector = pagObj.Drop(connectorMaster, 0, 0)
connector.Cells("BeginX").GlueTo(shpObj1.Cells("PinX"))
connector.Cells("EndX").GlueTo(shpObj2.Cells("PinX"))
doc.SaveAs(r'C:\utils\MyDrawing.vsd')
doc.Close()
appVisio.Visible =0
appVisio.Quit()
The RTF link is ignored - I tried that. Visio can add hyperlinks in the UI. So... does anyone know how I can add a link via Python here?
Visio only supports links on a shape (rather than within the text itself). A shape has Hyperlinks collection of Hyperlink objects and so you can add as follows:
# shpObj2.Text = """This is some more text. {\field{\*\fldinst HYPERLINK "http://www.google.com/"}{\fldrslt http://www.google.com}}"""
shpObj2.Text = "This is a shape with multiple links."
shp2Hyperlink1 = shpObj2.Hyperlinks.Add()
shp2Hyperlink1.Name = "Google"
shp2Hyperlink1.Address = "http://www.google.com"
shp2Hyperlink2 = shpObj2.Hyperlinks.Add()
shp2Hyperlink2.Name = "BBC"
shp2Hyperlink2.Address = "http://www.bbc.co.uk"
Under the covers this is just writing cells into the ShapeSheet:

Categories

Resources