正如標題所說,我只是無法讓 Python 腳本在 Adob??e 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 和 AppleScript 都有檔案,但 Python 速度快,因此被推薦,因為它可以處理我電腦上的檔案打開和關閉。
我已經嘗試了行內陣列的幾乎所有排列:
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
這更有希望,因為它似乎是一個更具描述性的錯誤。另外,如果我錯了,請糾正我,但基本上,當使用 Win32com 與 A??PI 對話時,VSCode 沒有任何有用的提示,對吧?基本上,錯誤可能來自 Adob??e Illustrator,但沒有像其他版本的 Visual Studio 那樣有用的建議。
我也嘗試過使用 numpy 使用陣列,但這也不起作用。
import numpy as np
array1 = np.array([[20, 50], [30, 70]])
myLine.SetEntirePath(array1)
有什么問題?win32com和Illustrator之間是不是有什么誤解?這是一件非常基本的事情,獲取一個腳本來在 Illustrator 上的兩點之間畫一條線。
我也許可以在 Adob??e Illustrator 的打開實體上畫一條線,使用 Python 腳本抓取線段,然后對 API 喜歡將其陣列發送給它的格式進行逆向工程,但這可能有它自己的問題。
不應該有什么作業嗎?畢竟不是所有這些不同的語言最終都通過通用中間語言處理然后發送到 API 嗎?
有人可以提出一些解決方案嗎?先感謝您
uj5u.com熱心網友回復:
有趣的。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
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/416488.html
標籤:
