[python] Generate ConnectorShape with styling in Draw

Java, C++, C#, Delphi... - Using the UNO bridges
Locked
Louis Glandières
Posts: 2
Joined: Mon Jun 23, 2025 5:24 pm

[python] Generate ConnectorShape with styling in Draw

Post by Louis Glandières »

Hello everyone

I am making a tool to automatically generate diagrams in draw but I have issues applying properties to the ConnectorShape

I am using LibreOffice 24.2.3 Portable and the uno python library

I would like to make an arrow with dotted line connecting one shape to the other in a straight line
The code below allow to create the connector shape but the properties change have either no effect or causes an error

Code: Select all

def add_connector(self, rect_start, rect_end):
        """Create a connector between rect"""
        connector = self.document.createInstance("com.sun.star.drawing.ConnectorShape")

        # Ajouter le connecteur à la page de dessin
        self.page.add(connector)

        # Définir le type de connecteur
        connector.setPropertyValue("LineStyle", "DASH")
        connector.setPropertyValue("ConnectorType", "LINE")  # "Straight", "Curved", etc.
        connector.setPropertyValue("LineJoint", "ROUND")

        # Définir les flèches aux extrémités du connecteur
        #connector.setPropertyValue("EndArrow", 2)  # 2 pour une flèche pleine
        #connector.setPropertyValue("StartArrow", 0)  # 0 pour pas de flèche

        # Connecter le connecteur aux rectangles
        connector.StartShape = rect_start
        connector.EndShape = rect_end
        return connector
I have looked into https://www.openoffice.org/api/docs/com ... rties.html and https://www.openoffice.org/api/docs/com ... Shape.html but I can't figure out how it translates in python code

Do you know how to implement theses properties in python or any guide that will help me do so ?

Thanks for any help
Last edited by MrProgrammer on Fri Jul 18, 2025 9:26 pm, edited 1 time in total.
Reason: Lock dormant topic with no activity for two weeks
LibreOffice 24.2.3 Portable

Windows 11
User avatar
MrProgrammer
Moderator
Posts: 5300
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: [python] Generate ConnectorShape with styling in Draw

Post by MrProgrammer »

Louis Glandières wrote: Tue Jun 24, 2025 11:17 am I am using LibreOffice 24.2.3 Portable and the uno python library. I would like to make an arrow with dotted line connecting one shape to the other in a straight line.
Writing programs like this requires two types of knowledge:
• How to program in Python
• How to use the LibreOffice API

I will guess that you are familiar with the former and your trouble is with the latter. The API is essentially the same for every language. Once you have a working example in one language, you'll know what API calls are needed in a different language. In my experience you will find far more examples of Draw programming in Basic than you will in Python. And far more examples of using the API inside LibreOffice than calling the API from an external program. I searched for connector in the Macros and UNO API forum and found 67 matches, including this one.
[Solved] Change a line to a connector programmatically

I suspect that the example doesn't solve your problem with connectors, but at least you have a (hopefully) working program to start from. If that topic does not help you, try some searches yourself. I suggest you begin by developing a very simple program in Basic using the Basic examples in the forum. Almost everyone who writes programs which use the API finds they need an object inspector. These two object inspectors can be used with Basic. Maybe Python too but definitely Basic. You can use the one you prefer.
[Tutorial] Introduction into object inspection with MRI
X-Ray version 6

It may not be practical to use an object inspector with an external program, but you won't need it after you get your Basic program working. Then you should be able to convert the API calls in the Basic program to Python without much trouble. Python is a more sophisticated programming language, but for writing a simple proof-of-concept program mainly consisting of API calls, it hardly matters what language you use, since the difficult task is discovering how to use the API to accomplish the goal.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.6, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
Louis Glandières
Posts: 2
Joined: Mon Jun 23, 2025 5:24 pm

Re: [python] Generate ConnectorShape with styling in Draw

Post by Louis Glandières »

I will try to use that method

it's not very convenient as I never used Basic and don't have it setup on my computer

I have other aspects of the project to do first so I will put this topic as dropped until I have a solution

edit : how do I rename the post or add a tag to it ?
LibreOffice 24.2.3 Portable

Windows 11
User avatar
MrProgrammer
Moderator
Posts: 5300
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: [python] Generate ConnectorShape with styling in Draw

Post by MrProgrammer »

Louis Glandières wrote: Tue Jun 24, 2025 11:17 am I am using LibreOffice 24.2.3 Portable and the uno python library
Louis Glandières wrote: Fri Jun 27, 2025 12:44 pm it's not very convenient as I never used Basic and don't have it setup on my computer
If you installed LibreOffice you installed its Basic interpreter too. You will want to use that one since its StarBasic dialect has special features which sample macros on the forum will expect.
Help.gif
Help.gif (69.3 KiB) Viewed 988 times

You should be able to run any Basic macros which you find on the forum immediately though you have to ensure the macro is run from the proper environment. For example, a macro designed for a Draw document has to be run from Draw, not directly in the Basic interpreter since the Draw document isn't available to it there. I decline to help with writing macros, but there are thousands of forum posts about that, and others seem to have a lot of experience with macros.
OpenOffice.org Macros Explained

Louis Glandières wrote: Fri Jun 27, 2025 12:44 pm How do I rename the post or add a tag to it ?
I don't understand why you would want to rename an individual post in a topic. To rename the topic go to your first post in this topic and use its Edit Post button, the leftmost one at top right. The forum doesn't use tags, as I understand the term, but you can edit the topic's title and this will change how the topic appears in the list of active topics. You can easily find your own topics in the forum using Your Posts under Quick Links at the top.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.6, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
Jurassic Pork
Posts: 31
Joined: Wed Oct 25, 2017 7:55 am
Location: France

Re: [python] Generate ConnectorShape with styling in Draw

Post by Jurassic Pork »

Salut Louis,
it seems that it isn't easy to do because for dash it seems that you must fill the LineDash struct of the connector object and for the arrow,
a PolyPoligonBezierCoords struct (10 points for a Triangle Arrow)
it seems that the LineEndType is Deprecated and i haven't seen the EndArrow and StartArrow properties.
ConnectorProps.png
ConnectorProps.png (33.53 KiB) Viewed 787 times
Here is a python script to create two rectangle shapes and to rely them with a dash line .

Code: Select all

# coding: utf-8
from __future__ import unicode_literals
import uno
from com.sun.star.awt import Point, Size
from com.sun.star.drawing.ConnectorType import STANDARD
from com.sun.star.drawing.LineStyle import DASH
#from com.sun.star.drawing.PolygonArrowEndKind import ARROW

try:
    CTX = uno.getComponentContext()
    SM = CTX.getServiceManager()
    desktop = XSCRIPTCONTEXT.getDesktop()
    ODOC = desktop.getCurrentComponent()
except:
    print("Module  init Error")

def connect_two_rect(*args):
    if not ODOC.supportsService("com.sun.star.drawing.DrawingDocument"):
        print("Ce script doit être exécuté dans un document Draw.")
        return
    page = ODOC.getDrawPages().getByIndex(0)
    # Créer le premier rectangle
    shape1 = ODOC.createInstance("com.sun.star.drawing.RectangleShape")
    shape1.Position = Point(2000, 2000)   # 2cm . 2cm
    shape1.Size = Size(3000, 2000)        # 3cm x 2cm
    page.add(shape1)
    # Créer le second rectangle
    shape2 = ODOC.createInstance("com.sun.star.drawing.RectangleShape")
    shape2.Position = Point(10000, 2000)  # 10cm . 2cm
    shape2.Size = Size(3000, 2000)        # 3cm x 2cm
    page.add(shape2)
    # Créer un connecteur
    connector = ODOC.createInstance("com.sun.star.drawing.ConnectorShape")
    connector.setPropertyValue("LineStyle", DASH)
    connector.setPropertyValue("LineDashName", "Dash")
    dash =  uno.createUnoStruct("com.sun.star.drawing.LineDash")
    dash.Dashes = 2
    dash.Distance = 200
    dash.DashLen = 300
    connector.LineDash = dash
    connector.setPropertyValue("LineWidth", 50)  
    connector.setPropertyValue("LineColor", 16538624) # rouge
    page.add(connector)
    # Relier le connecteur aux rectangles
    connector.setPropertyValue("StartShape", shape1)
    connector.setPropertyValue("EndShape", shape2)
    # glue points top: 0 - left: 1 - bottom: 2 - right: 3
    connector.setPropertyValue("StartGluePointIndex", 1)  # it seems that 1 is right
    connector.setPropertyValue("EndGluePointIndex", 3)    # it seems that 3 is left

g_exportedScripts = connect_two_rect, 
connect2rect.gif
connect2rect.gif (13.54 KiB) Viewed 787 times
Ami calmant, J.P
OpenOffice 4.1.14 , LibreOffice 7.6.2.1 on Windows 11/ LibreOffice 7.3.7 on Lubuntu 22.04
cwolan
Posts: 182
Joined: Sun Feb 07, 2021 3:44 pm

Re: [python] Generate ConnectorShape with styling in Draw

Post by cwolan »

Jurassic Pork wrote: Thu Jul 03, 2025 6:35 am it seems that it isn't easy to do because for dash it seems that you must fill the LineDash struct of the connector object and for the arrow,
a PolyPoligonBezierCoords struct (10 points for a Triangle Arrow)
it seems that the LineEndType is Deprecated and i haven't seen the EndArrow and StartArrow properties.
What happens if you add the following line to your script?

Code: Select all

connector.setPropertyValue("LineEndName", "Arrow")
OpenOffice 1.1.5 – 4.1.15
LibreOffice 3.3.0.4 – 25.2
Windows 7,10,11 64-bit
User avatar
Jurassic Pork
Posts: 31
Joined: Wed Oct 25, 2017 7:55 am
Location: France

Re: [python] Generate ConnectorShape with styling in Draw

Post by Jurassic Pork »

cwolan wrote: Thu Jul 03, 2025 9:48 pm What happens if you add the following line to your script?

Code: Select all

connector.setPropertyValue("LineEndName", "Arrow")
Hello,
it doen't work for me with LibreOffice 7.6 Windows. But i have found a solution -> using Style

1 - Define a new style for your connector (ex : Connecteur)
ConnecteurStyle.png
ConnecteurStyle.png (47.15 KiB) Viewed 708 times
2 - New code to apply style :

Code: Select all

def connect_two_rect(*args):
    if not ODOC.supportsService("com.sun.star.drawing.DrawingDocument"):
        print("Ce script doit être exécuté dans un document Draw.")
        return
    page = ODOC.getDrawPages().getByIndex(0)
    family = ODOC.getStyleFamilies().getByIndex(0)  # graphics family
    style = family.getByName("Connecteur") # get style
    # Créer le premier rectangle
    shape1 = ODOC.createInstance("com.sun.star.drawing.RectangleShape")
    shape1.Position = Point(2000, 2000)   # 2cm . 2cm
    shape1.Size = Size(3000, 2000)        # 3cm x 2cm
    page.add(shape1)
    # Créer le second rectangle
    shape2 = ODOC.createInstance("com.sun.star.drawing.RectangleShape")
    shape2.Position = Point(10000, 2000)  # 10cm . 2cm
    shape2.Size = Size(3000, 2000)        # 3cm x 2cm
    page.add(shape2)
    # Créer un connecteur
    connector = ODOC.createInstance("com.sun.star.drawing.ConnectorShape")
    page.add(connector)
    connector.Style = style # appliquer un style au connecteur
    # Relier le connecteur aux rectangles
    connector.setPropertyValue("StartShape", shape1)
    connector.setPropertyValue("EndShape", shape2)
    # glue points top: 0 - left: 1 - bottom: 2 - right: 3
    connector.setPropertyValue("StartGluePointIndex", 1)  # it seems that 1 is right
    connector.setPropertyValue("EndGluePointIndex", 3)    # it seems that 3 is left

g_exportedScripts = connect_two_rect, 
ConnectorRects.gif
ConnectorRects.gif (53.08 KiB) Viewed 708 times
Friendly, J.P
OpenOffice 4.1.14 , LibreOffice 7.6.2.1 on Windows 11/ LibreOffice 7.3.7 on Lubuntu 22.04
cwolan
Posts: 182
Joined: Sun Feb 07, 2021 3:44 pm

Re: [python] Generate ConnectorShape with styling in Draw

Post by cwolan »

Jurassic Pork wrote: Fri Jul 04, 2025 9:36 am
cwolan wrote: Thu Jul 03, 2025 9:48 pm What happens if you add the following line to your script?

Code: Select all

connector.setPropertyValue("LineEndName", "Arrow")
Hello,
it doen't work for me with LibreOffice 7.6 Windows.
That's possible.
On the other hand, BASIC macro seems to work with LibreOffice 7.6.2.1, 24.8.7.2 and 25.2.4.3 on Windows 11.
drawFICHIER.odg
(14.77 KiB) Downloaded 2 times
Jurassic Pork wrote: Fri Jul 04, 2025 9:36 am But i have found a solution -> using Style
Interesting…
OpenOffice 1.1.5 – 4.1.15
LibreOffice 3.3.0.4 – 25.2
Windows 7,10,11 64-bit
User avatar
Jurassic Pork
Posts: 31
Joined: Wed Oct 25, 2017 7:55 am
Location: France

Re: [python] Generate ConnectorShape with styling in Draw

Post by Jurassic Pork »

cwolan wrote: Fri Jul 04, 2025 11:35 am
Jurassic Pork wrote: Fri Jul 04, 2025 9:36 am
cwolan wrote: Thu Jul 03, 2025 9:48 pm What happens if you add the following line to your script?

Code: Select all

connector.setPropertyValue("LineEndName", "Arrow")
Hello,
it doen't work for me with LibreOffice 7.6 Windows.
That's possible.
On the other hand, BASIC macro seems to work with LibreOffice 7.6.2.1, 24.8.7.2 and 25.2.4.3 on Windows 11.
Sorry, it works also with python code ( my code was wrong )
OpenOffice 4.1.14 , LibreOffice 7.6.2.1 on Windows 11/ LibreOffice 7.3.7 on Lubuntu 22.04
Locked