How to connect 2 shapes with elbow arrow connector in aspose.cells python

is it possible to connect 2 shapes using connector in aspose cells like aspose diagram ,any option to solve these

@sayen_vv

To connect two shapes with an elbow arrow connector in Aspose.Cells using Python, you can utilize the add_connector method. This method allows you to create a new connector shape and add it to your collection of shapes.

Here’s a basic example of how to do this:

import aspose.cells as cells

# Create a new workbook
workbook = cells.Workbook()

# Access the first worksheet
worksheet = workbook.get_worksheets().get(0)

# Define the positions and dimensions for the two shapes
shape1_x = 100
shape1_y = 100
shape1_width = 100
shape1_height = 50

shape2_x = 300
shape2_y = 100
shape2_width = 100
shape2_height = 50

# Add two rectangles as shapes
shape1 = worksheet.get_shapes().add_shape(cells.ShapeType.RECTANGLE, shape1_x, shape1_y, shape1_width, shape1_height)
shape2 = worksheet.get_shapes().add_shape(cells.ShapeType.RECTANGLE, shape2_x, shape2_y, shape2_width, shape2_height)

# Add an elbow connector between the two shapes
connector_index = worksheet.get_shapes().add_connector(cells.ShapeType.ELBOW_CONNECTOR, shape1_x + shape1_width, shape1_y + shape1_height / 2, shape2_x - shape1_x - shape1_width, shape2_y - shape1_y)

# Save the workbook
workbook.save("shapes_with_connector.xlsx")

In this example:

  • Two rectangle shapes are created at specified positions.
  • An elbow connector is added between the two shapes using the add_connector method, specifying the type of connector and its position based on the dimensions of the shapes.

Make sure to adjust the coordinates and dimensions according to your specific layout requirements. This approach allows you to visually connect shapes in your Excel document programmatically.

For more details on the add_connector method and its parameters, you can refer to the Aspose documentation here.

Hi @Professionalize.Discourse ,
Thank you for your response. However, the documentation you shared is from Aspose.Slides. I also checked for the add_connector method in Aspose.Cells, and it appears that such a method does not exist there.

@sayen_vv,

There is no automatic option to use it for connecting two shapes in one go. You need to use line shape or relevant shapes to draw connection between two shapes in code by yourselves. See the following sample code for your reference.

# Create a new workbook
workbook = Workbook()
sheet = workbook.worksheets[0]

# Add two shapes (e.g., rectangles)
shape1 = sheet.shapes.add_rectangle(2, 0, 2, 0, 100, 60)
shape2 = sheet.shapes.add_rectangle(12, 8, 12, 8, 100, 60)


shape1_center_x = shape1.upper_left_column * 64 + shape1.left + shape1.width // 2
shape1_center_y = shape1.upper_left_row * 20 + shape1.top + shape1.height // 2

shape2_center_x = shape2.upper_left_column * 64 + shape2.left + shape2.width // 2
shape2_center_y = shape2.upper_left_row * 20 + shape2.top + shape2.height // 2

line = sheet.shapes.add_line(2, 0, 2, 0, 100, 100)

line.top = shape1_center_y
line.left = shape1_center_x
line.width = shape2_center_x - shape1_center_x
line.height = shape2_center_y - shape1_center_y

# Save the file
workbook.save("e:\\test2\\shapes_connected_with_line.xlsx")

Hope, this helps a bit.

Also, see the documents on drawing shapesin the section for your reference.
http://docs.aspose.com/cells/python-net/insert-shapes/