简体   繁体   English

如何在python-pptx中获取给定形状的连接点坐标

[英]How to get co-ordinates of connecting points for a given shape in python-pptx

My task is to connect an arrow/line from shape1 to shape2.我的任务是连接从 shape1 到 shape2 的箭头/线。 While doing so, I need to get the exact co-ordinates of connecting points along the perimeter of shape.这样做时,我需要获得沿形状周边的连接点的精确坐标。 To be clear, by connecting points I mean the small green bubbles you see along the perimeter of a shape when you try to connect an arrow/line to it in the powerpoint tool.需要明确的是,连接点是指当您尝试在 powerpoint 工具中将箭头/线连接到形状时沿形状周边看到的绿色小气泡。 I am interested only in these points as the PPT software adds visually appealing alignments if connected to those points.我只对这些点感兴趣,因为如果连接到这些点,PPT 软件会添加视觉上吸引人的对齐方式。

In existing code, there are two functions _move_begin_to_cxn() and _move_end_to_cxn() , which share common logic, where they get the connecting points' coordinates.在现有代码中,有两个函数_move_begin_to_cxn()_move_end_to_cxn() ,它们共享共同的逻辑,它们获取连接点的坐标。 But this works only in case of a rectangle.但这仅适用于矩形的情况。 This is since we only have top-left location and width-height of the shape.这是因为我们只有形状的左上角位置和宽度高度。

    def _move_end_to_cxn(self, shape, cxn_pt_idx):
        """
        Move the end point of this connector to the coordinates of the
        connection point of *shape* specified by *cxn_pt_idx*.
        """
        x, y, cx, cy = shape.left, shape.top, shape.width, shape.height
        self.end_x, self.end_y = {
            0: (int(x + cx / 2), y),
            1: (x, int(y + cy / 2)),
            2: (int(x + cx / 2), y + cy),
            3: (x + cx, int(y + cy / 2)),
        }[cxn_pt_idx]

This is difficult to compute for complicated shapes - such as oval, pentagon, parallelogram - with just the top-left location and width-height.对于仅具有左上角位置和宽高的复杂形状(例如椭圆形、五边形、平行四边形),这很难计算。

Is there any efficient way to get the accurate location of the connectable points?是否有任何有效的方法来获得可连接点的准确位置? Any help would be appreciated.任何帮助,将不胜感激。

Thanks in advance!提前致谢!

There is no smooth equation to get those points for an arbitrary shape.没有平滑的方程可以得到任意形状的这些点。 PPT hard-codes those points, parameterized to the bounding box, and simply activates them when appropriate. PPT 对这些点进行硬编码,参数化为边界框,并在适当的时候简单地激活它们。

Fortunately, you don't have to compute them to thirteen significant figures.幸运的是,您不必将它们计算为 13 个有效数字。 One simple-but-tedious way to get the points is to hard-code them yourself.获得分数的一种简单但乏味的方法是自己对它们进行硬编码。 Create a desired shape in PPT, enlarge it as far as you can, and read the relative coordinates from that enlargement.在 PPT 中创建一个想要的形状,尽可能地放大它,然后读取放大后的相对坐标。 Store those parameters in a list with each shape.将这些参数存储在每个形状的列表中。

Another straightforward way is to examine the connection points for each shape;另一种直接的方法是检查每个形状的连接点; they're all extreme points and midpoints of edges or arcs.它们都是边或弧的极值点和中点。 Use basic analytic geometry to compute the relative coordinates of each point.使用基本解析几何计算每个点的相对坐标。 Again, hard-code these into your application.同样,将这些硬编码到您的应用程序中。

Whichever way you pick, you might be off by a pixel or two, but that will be close enough for PPT to recognize that you want the connection.无论您选择哪种方式,您可能会偏离一两个像素,但这足以让 PPT 识别您想要连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从矩形中定义 2 个坐标(python) - Defining 4 co-ordinates from 2 in a rectangular shape (python) Konvajs:如何从画布对象中获取与画布坐标相对应的弓箭手坐标? - Konvajs : how to get bowser co-ordinates corresponding to canvas co-ordinates from canvas object? 如何使用 python-pptx 从形状的连接点到另一个形状的连接点画一条线? - How can I draw a line from shape's connecting point to another shape's connecting point using python-pptx? 给定 n 个城市的 x,y 坐标,如何使用 python 形成距离矩阵? - Given the x,y co-ordinates of n cities how to form the distance matrix using python? 如何在python中使用OCR获取从Image识别的文本坐标 - How to get the co-ordinates of the text recogonized from Image using OCR in python 如何使用Tweepy和Python将坐标发送到Twitter状态更新 - How to send co-ordinates with Tweepy and Python to Twitter status update Python Web抓取-如何找到map元素的坐标? - Python Web scraping - How to locate the Co-ordinates of the map element? 如何在 Opencv Python 中查找特定地标点的坐标 - How to find co-ordinates of a specific Landmark point in Opencv Python python - 如何将形状复制到python-pptx中的其他幻灯片中? - How to copy a shape into other slide in python-pptx? python-pptx - 如何使用自定义 rgb colors 正确填充形状? - python-pptx - how to correctly fill in a shape with custom rgb colors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM