scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.4k stars 520 forks source link

How do I adjust an elbow connector? #1017

Open Ozymansour opened 4 days ago

Ozymansour commented 4 days ago

The following code creates a connector starting at the top right of a table and ending at a country on a map:

def add_connector(slide, end_shape, start_table, is_left_side: bool, header_textbox):
    _, (end_left, end_top, end_width, end_height) = end_shape
    end_center = Emu(round(end_left + (end_width / 2))), Emu(
        round(end_top + (end_height / 2))
    )
    start_x = Emu(
        start_table.left + start_table.width if is_left_side else start_table.left
    )
    start_y = Emu(start_table.top + (start_table.height / 2) - Cm(1.25))

    connector = slide.shapes.add_connector(
        MSO_CONNECTOR.ELBOW, start_x, start_y, *end_center
    )
    if is_left_side:
        connector.begin_connect(header_textbox, 3)
    else:
        connector.begin_connect(header_textbox, 1)
    print(connector.adjustments)
    connector.line._get_or_add_ln().append(
        parse_xml(
            '<a:tailEnd type="oval" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>'
        )
    )
    connector.line.width = Pt(0.5)
    connector.line.color.rgb = RGBColor(32, 64, 140)

    return connector

This is the current output:
CleanShot 2024-10-05 at 13 06 24@2x

My issue is that the elbow connector always goes down first and then right, instead of just simply going down like so: CleanShot 2024-10-05 at 13 07 03@2x

This means i'm forced to manually drag that yellow adjustment box in the middle (to the right in this case) to get the desired look. Unfortunately this is quite tiresome when I have many connectors on a deck of slides.

Is there any way to get the connector to display like above instead of it taking many twists and turns?

scanny commented 4 days ago

The "little-yellow-box" things are adjustments. The documentation talks about those here: https://python-pptx.readthedocs.io/en/latest/user/autoshapes.html#adjusting-an-autoshape

Ozymansour commented 4 days ago

The "little-yellow-box" things are adjustments. The documentation talks about those here: https://python-pptx.readthedocs.io/en/latest/user/autoshapes.html#adjusting-an-autoshape

Is a connector an auto-shape? After adding adjs = connector.adjustments to the function above, I keep getting this error: AttributeError: 'Connector' object has no attribute 'adjustments'

scanny commented 2 days ago

Ah, yes, apologies, you're quite right. A connector is a cxnSp element, not an sp element and so doesn't have that attribute.

I believe that attribute could be implemented on Connector but hasn't been yet.