繁体   English   中英

如何获取 Python 脚本以在 Adobe Illustrator 和 Visual Studio Code 中绘制简单的线条

[英]How do I get Python Script to draw a simple line in Adobe Illustrator and Visual Studio Code

就像标题说的那样,我只是无法让 Python 脚本在 Adobe Illustrator 中的两点之间画一条线。 以下代码不断产生错误:

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

由于想在此过程中学习 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.

我已经尝试了行内数组的几乎所有排列:

myLine.SetEntirePath([[10.,20.], [30, 70]]) #fails here

但没有任何效果。 通常我得到错误,

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> 

这表明数组格式不正确,但对我来说看起来不错。 我几乎尝试了所有可能性:没有方括号,没有外括号。 根据我对 Python 的了解,这是具有两个 X、Y 对的数组从两点画一条简单线的正确格式。

当我尝试

myLine.SetEntirePath([10.,20.], [50., 100.], [100, 30])

我得到一个不同的错误:

TypeError: SetEntirePath() takes from 1 to 2 positional arguments but 4 were given

这更有希望,因为它似乎是一个更具描述性的错误。 另外,如果我错了,请纠正我,但基本上,VSCode 在使用 Win32com 时没有有用的提示,因为它与 API 对话,对吧? 基本上,错误可能来自 Adobe Illustrator,但没有像其他版本的 Visual Studio 那样有用的建议。

我也尝试过使用 numpy 的数组,但这也不起作用。

import numpy as np
array1 = np.array([[20, 50], [30, 70]])
myLine.SetEntirePath(array1)

有什么问题? win32com和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问题。

不应该有什么工作吗? 毕竟不是所有这些不同的语言最终都通过通用中间语言进行处理,然后发送到 API 吗?

有人可以提出一些解决方案吗? 先感谢您

有趣的。 确实, SetEntirePath()方法可能已损坏或其他原因。 我也没有设法让它与 Python 一起工作。

但是有一个解决方法。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM