shape.SetPosition(x, y, anchor)
移动。默认锚点是左下角,第 3 个参数可指定锚点。代码里 img.SetPosition((w - img.SizeWidth)/2, (h - img.SizeHeight)/2) 就是把图居中放进底板。
签名
shape.SetPosition(x, y, anchor)
说明
移动。默认锚点是左下角,第 3 个参数可指定锚点。代码里
img.SetPosition((w - img.SizeWidth)/2, (h - img.SizeHeight)/2) 就是把图居中放进底板。Python 示例(可直接运行)
Python
shape = app.ActiveSelectionRange(1)
# SetPosition(x, y, 锚点):默认按左下角定位
shape.SetPosition(20 / 25.4, 30 / 25.4)
# 想把对象【中心】放到某点,就减去半个宽高
W, H = 210 / 25.4, 297 / 25.4 # 页面宽高
shape.SetPosition((W - shape.SizeWidth) / 2, (H - shape.SizeHeight) / 2)
print("已水平垂直居中")
关于本类:Shape 几何与变换
位置、尺寸、旋转、包围盒。全部跟随 doc.Unit(你的环境是英寸),字号例外。
- 本类共 17 条,这是其中之一。
- 完整分类见:Shape 几何与变换
同类其他接口
- shape.Left / .Top / .Right / .Bottom四边坐标。Top > Bottom(Y 轴向上)。群组内部的子对象读不到,得用 GetBoundingBox。
- shape.PositionX / .PositionY左下角坐标,直接读写比 SetPosition 少一层调用。
- shape.SizeWidth / .SizeHeight宽度 / 高度,读写。缩放时先读原尺寸再按比例算新尺寸。
- shape.CenterX / .CenterY中心点坐标,定位对齐时比 Left/Top 好用。
- shape.RotationAngle旋转角度(度),绕 RotationCenterX/Y 转。
- shape.SetSize(w, h, anchor)缩放,默认锚点左上。等比例缩放要自己算:s = max(W/ow, H/oh); img.SetSize(ow*s, oh*s)。
- shape.Move(dx, dy)相对位移,比 SetPosition 省一次读坐标。
- shape.Rotate(angle)相对旋转指定角度。