How do I get Python Script to draw a simple line in Adobe Illustrator and Visual Studio Code
Like the title says, I just can't get Python script to draw a line between two points in Adobe Illustrator. The following code keeps generating errors:
from os import linesep import win32com.client as win32com app = win32com.GetActiveObject("Illustrator.Application") docRef = app.Documents.Add() app.Documents.Add() myLine = docRef.PathItems.Add() myLine.Stroked = True myLine.StrokeWidth =5 myLine.SetEntirePath([[10.,20.], [30, 70]]) #fails here
I have tried just about every permutation of the array inside the line that says:
myLine.SetEntirePath([[10.,20.], [30, 70]]) #fails here
but nothing works. Usually I get the error,
Traceback (most recent call last): File "c:\Users\User\Documents\code\python_illustrator_api\test_com_interface.py", line 41, in <module> myLine.SetEntirePath([[10.,20.], [30, 70]]) File "<COMObject Add>", line 2, in SetEntirePath pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Adobe Illustrator', 'Illegal argument - argument 1\n- Only arrays with dimension 1 are supported', None, 0, -2147220262), None) PS C:\Users\User\Documents\code\python_illustrator_api>
This is suggestive that the array is not formatted correctly, but it looks good to me. I have tried just about every possibility: No square brackets, no outer brackets. From what I know of Python, this is the correct format for an array with two X,Y pairs to draw a simple line from the two points.
When I try
myLine.SetEntirePath([10.,20.], [50., 100.], [100, 30])
I get a different error:
TypeError: SetEntirePath() takes from 1 to 2 positional arguments but 4 were given
That is a little more promising, because it seems to be a more descriptive error. Also, correct me if I'm wrong, but basically, VSCode has no helpful prompting when using Win32com as it talks to an API, right? Basically, errors can come from Adobe Illustrator, but there are no helpful suggestions like other versions of Visual Studio.
I have also tried using an array using numpy, but that doesn't work either.
import numpy as np array1 = np.array([[20, 50], [30, 70]]) myLine.SetEntirePath(array1)
What's the problem? Is there some kind of misunderstanding between win32com and Illustrator? This is a really basic thing to do, get a script to draw a line between two points on Illustrator.
I could perhaps draw a line on an open instance of Adobe Illustrator, grab the line-segment using Python script, and then kind of reverse-engineer the format that the API likes to have its arrays sent to it, but that might have it's own problems.
Shouldn't something work? After all don't all these different languages end up getting processed through the Common Intermediate Language and then send to the API?
Can someone suggest some solution to this? Thank you in advance
Interesting. Probably the
SetEntirePath()method is broken or something, indeed. I didn't manage to get it work with Python as well.
But there is a workaround.
pathItems.pathPointscollection has the method
Add(), so you can use a loop to add any points with given coordinates to the path this way:
import win32com.client as win32com app = win32com.GetActiveObject("Illustrator.Application") doc = app.Documents.Add() myLine = doc.pathItems.Add() myLine.stroked = True myLine.strokeWidth = 5 points = [[10, 20], [50, 100], [100, 30]] for xy in points: point = myLine.pathPoints.Add() point.anchor = xy point.leftDirection = xy point.rightDirection = xy