[英]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.就像标题说的那样,我只是无法让 Python 脚本在 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
There are other options that I prefer not to use, due to wanting to learn Python in this process.由于想在此过程中学习 Python,因此还有其他选项我不想使用。 VBScript, JavaScript and AppleScript have documentation, but Python has the speed and is recommended because it can handle opening and closing files on my computer.
VBScript, JavaScript and AppleScript have documentation, but Python has the speed and is recommended because it can handle opening and closing files on my computer.
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.
根据我对 Python 的了解,这是具有两个 X、Y 对的数组从两点画一条简单线的正确格式。
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?
另外,如果我错了,请纠正我,但基本上,VSCode 在使用 Win32com 时没有有用的提示,因为它与 API 对话,对吧? Basically, errors can come from Adobe Illustrator, but there are no helpful suggestions like other versions of Visual Studio.
基本上,错误可能来自 Adobe Illustrator,但没有像其他版本的 Visual Studio 那样有用的建议。
I have also tried using an array using numpy, but that doesn't work either.我也尝试过使用 numpy 的数组,但这也不起作用。
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?
win32com和Illustrator之间是不是有什么误解? This is a really basic thing to do, get a script to draw a line between two points on Illustrator.
这是一件非常基本的事情,获取一个脚本来在 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. 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问题。
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?
毕竟不是所有这些不同的语言最终都通过通用中间语言进行处理,然后发送到 API 吗?
Can someone suggest some solution to this?有人可以提出一些解决方案吗? Thank you in advance
先感谢您
Interesting.有趣的。 Probably the
SetEntirePath()
method is broken or something, indeed.确实,
SetEntirePath()
方法可能已损坏或其他原因。 I didn't manage to get it work with Python as well.我也没有设法让它与 Python 一起工作。
But there is a workaround.但是有一个解决方法。
pathItems.pathPoints
collection has the method Add()
, so you can use a loop to add any points with given coordinates to the path this way: pathItems.pathPoints
集合具有Add()
方法,因此您可以使用循环将具有给定坐标的任何点添加到路径中:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.