Python win32com和二维数组 [英] Python win32com and 2-dimensional arrays

查看:73
本文介绍了Python win32com和二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python和win32com自动化软件形式时,Adobe会遇到传递2D坐标数组的问题.如果看一下Adobe为Visual Basic(VB)发行的代码,那很简单.在Illustrator中绘制线条的简化示例如下所示:

When using python and win32com to automate the software form Adobe one encounters problem with passing arrays of 2d coordinates. If one looks at code that Adobe ships for visual basic (VB) its simple. A simplified example for drawing a line in Illustrator would look as follows:

Set appObj = CreateObject("Illustrator.Application")
Set docObj = appObj.Documents.Add

Set pathItem = docObj.PathItems.Add
    pathItem.SetEntirePath Array(Array(0.0, 0.0), Array(20.0, 20.0))

现在,天真的假设是可以通过如下转换将VB代码变成python:

Now the naive assumption is that VB code could just just be turned into python by converting it as follows:

from win32com.client import Dispatch

appObj = Dispatch("Illustrator.Application")  
docObj = appObj.Documents.Add()

pathItem = docObj.PathItems.Add()
pathItem.SetEntirePath( [ [0.0,0.0], [20.0,20.0] ] )

显然,这并不容易,python发出一个错误,指出仅支持维数为1的数组".现在我知道数组数组和二维数组之间存在区别.所以问题是我如何强制python制作正确类型的数组?

Obviously, it's not that easy, python issues a error saying "Only arrays with dimension 1 are supported". Now i know that there's a difference between arrays of arrays and 2 dimensional arrays. So the question is how do i force python to make a array of the right kind?

我尝试制作自己的VARIANT类型,但失败了. Ive为此也研究了ctypes.任何人都遇到过同样的问题,可能会有所启发吗?

I tried making my own VARIANT type but failed miserably. Ive also looked at ctypes for this. Anybody have had the same problem and could shed some light?

PS:

我知道可以使用以下方法来解决该问题:

I am aware that the problem can be circumvented by using:

pathItem = docObj.PathItems.Add()
for point in (  [0.0,0.0], [20.0,20.0] ):
    pp = pathItem.PathPoints.Add() 
    pp.Anchor = point

但是在类似的情况下,这实际上是行不通的.无论如何,关键是要编写有关移植到学生的指南,这样与原始意图尽可能地接近会更好.

But there are similar cases where this does not in fact work. Anyway the point is to write guidelines on porting to students so being as close to the original intent would be better.

推荐答案

在尝试使用win32com确定选择区域时,我遇到了同样的问题.我发现使用comtypes而不是win32com访问photoshop可以彻底解决多维数组问题.

I encountered this same issue when trying to determine selection areas using win32com. I found that using comtypes rather than win32com to access photoshop solved the multidimensional array problem outright.

我认为一维数组问题是win32com的局限性,但我可能是错的.

I believe that the one dimensional array problem is a limitation of win32com, but I may be wrong.

您可以在这里获得型号: http://sourceforge.net/projects/comtypes/

You can get comtypes here: http://sourceforge.net/projects/comtypes/

技术艺术家组织上有关于此问题的帖子值得一看.

There is a post about this issue on Tech Artists Org which is worth a look through.

这里是一个使用上面链接的技术艺术家的论坛帖子中的comtypes通过数组传递的示例.路径点的实现应相似.

Here is an example of passing through an array using comtypes from the tech artist's forum post linked above. The implementation for path points should be similar.

import comtypes.client
ps_app = comtypes.client.CreateObject('Photoshop.Application') 
# makes a new 128x128 image
ps_app.Documents.Add(128, 128, 72)

# select 10x10 pixels in the upper left corner
sel_area = ((0, 0), (10, 0), (10, 10), (0, 10), (0, 0))
ps_app.ActiveDocument.Selection.Select(sel_area)

这篇关于Python win32com和二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆