简体   繁体   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

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.

相关问题 如何在 Visual Studio Code 上使用 python 海龟图形绘制递归谢尔宾斯基箭头曲线 - How do I draw recursive Sierpiński arrowhead curve using python turtle graphics on Visual Studio Code 为什么在 Visual Studio Code 上运行 Python 3 脚本时会出现 SyntaxError? - Why do I get SyntaxError when running a Python 3 script on Visual Studio Code? 在Visual Studio Code上调试Python时,如何更改当前行? - How do I change the current line when debugging Python on Visual Studio Code? 在 Visual Studio Code 中运行 python 脚本; 如何让 `input ()` 工作? - Running python script in Visual Studio Code; how to get `input ()` to work? 使用 os.getcwd() 时,如何将 Visual Studio 代码中的 Python 和 Python 转换为 output sem? - How do I get Python and Python in Visual Studio code to output the sem when using os.getcwd()? 如何检测使用python脚本安装的Visual Studio版本 - How do I detect the version of visual studio installed with a python script 如何在Visual Studio Code上执行Python脚本 - How to execute a Python script on Visual Studio Code 如何在Python3中使用Kivy绘制一条简单的线和一个矩形? - How do I draw a simple line and a rectangle using Kivy in Python3? C#代码无法在Visual Studio中运行完整的python脚本 - C# code do not run complete python script in visual studio 使用Visual Studio Code编写Python代码时,为什么会收到2条错误消息? - Why do I get 2 error messages when writing Python code using Visual Studio Code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM