0

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 enter image description here

Related documentations and forums that I found:

  1. http://www.44342.com/visio-f963-t1206-p1.htm

  2. https://learn.microsoft.com/en-us/office/vba/api/visio.selection.align

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Your solution works perfectly. Thanks a lot. Could you please help me what steps did you do to find a solution :) I'm also searching docs but couldn't find smt like "Para.HorzAlign"
It's simple. Launched the Visio macro recorder, did the text alignment. Received a code like Application.ActiveWindow.Page.Shapes.ItemFromID (1) .CellsSRC(visSectionParagraph, 0, visHorzAlign) .FormulaU =" 2 ". Then using Debug.Print ActivePage.Shapes.ItemFromID(1).CellsSRC (visSectionParagraph, 0, visHorzAlign).Name got Para.HorzAlign. Essentially, the macro recorder is a handy help system.
I got the idea. I did the same steps as you recommended, and got the result, thank you very much. For my future projects, in addition to Python, I will try to learn some VBA :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.