Python win32com and 2-dimensional arrays

When using python and win32com to automate form software, Adobe is faced with a problem with transmitting arrays from 2d coordinates. If you look at the code that Adobe sends for Visual Basic (VB), it's simple. A simplified example of drawing a line in Illustrator is 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))

Now the naive assumption is that VB code can simply 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] ] )

Obviously, this is not so simple, python throws an error: "Only arrays with a size of 1 are supported." Now I know that there is a difference between arrays of arrays and 2-dimensional arrays. So the question is how to get python to create an array of the type you want?

VARIANT, . Ive ctypes . - ?

PS:

, , :

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

, . , , , , .

+3
2

win32com. , comtypes, win32com , .

, win32com, .

comtypes : http://sourceforge.net/projects/comtypes/

Tech Orists, .

comtypes Tech Artist, . .

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)
+2

, win32com. , , Photoshop - . . , SolidWorks, . win32com :

from win32com.client import VARIANT
from pythoncom import VT_VARIANT

def variant(data):
    return VARIANT(VT_VARIANT, data)

, , python :

import collections

def vararr(*data):
    if (  len(data) == 1 and 
          isinstance(data, collections.Iterable) ):
        data = data[0]
    return map(variant, data)

, :

from win32com.client import Dispatch, VARIANT
from pythoncom import VT_VARIANT
import collections


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

def variant(data):
    return VARIANT(VT_VARIANT, data)

def vararr(*data):
    if (  len(data) == 1 and 
          isinstance(data, collections.Iterable) ):
        data = data[0]
    return map(variant, data)

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

#or you can have a iterable of iterables
pathItem = docObj.PathItems.Add()
pathItem.SetEntirePath( vararr( [[30.0,10.0], [60.0,60.0]] )  )

, comtypes, , win32com. , win32com, , . , . , -.

+7

All Articles