It looks like you're new here. If you want to get involved, click one of these buttons!
that depends how the ports are marked. Usually, there is a label on a port shape or the port shapes are on special layers.
If you have a label, a feasible solution would be to look for shapes under the label using the region query using either Shapes.each_overlapping (cell level) or Layout.begin_shapes_overlapping (hierarchical). In either case, you'll have to feed the bounding box of the text (which is basically a box consisting of a single point - i.e. left == right and top == bottom) into the search function as the lookup region. You'll receive a list of shapes which you could consider port shapes to route to.
That's just a sketch. As said before, the solution strongly depends on how the ports are marked. In some cases, the ports may be marked by pin shapes (i.e. in Cadence) which is put on certain layers. That's the easiest case because you'll just have to look for such shapes.
Do you reach some solution? I want to do the same but it seems that it is not straight forward. I can obtain the points to route externally to klayout or with the mouse.