You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python脚本调用Illustrator保存EPS文件遇COM Variant转换错误

Python调用Illustrator保存EPS文件时的COM类型转换错误

我想用Python将Illustrator文件保存为.eps格式。由于Illustrator原生不支持Python,我下载了一个参考文件导入代码中,代码逻辑和Illustrator里的JavaScript基本一致,但运行时遇到了类型转换错误。

错误信息

Traceback (most recent call last):
File "s:\Workstation - DavidK\Code\Main\SAVETEST.py", line 12, in <module>

doc.SaveAs(SaveIn = 'S:/Workstation - DavidK/Code/Main/DeleteLater/tempSheet.eps', Options = saveOpts)

File "s:\Workstation - DavidK\Code\Main\illustrator_ref.py", line 1998, in SaveAs

 

return self._oleobj_.InvokeTypes(1398161747, LCID, 1, (24, 0), ((8, 1), (12, 17)),SaveIn

 TypeError: Objects of type 'EPSSaveOptions' can not be converted to a COM VARIANT (but obtaining the buffer() of this object could)

我尝试过用buffer()方法,但Python3里需要用memoryview,而它要求传入bytes类型,完全不知道该怎么操作。

我的代码

from illustrator_ref import *
import win32com.client as win32
from win32com.client import DispatchBaseClass
from win32com.client import CoClassBaseClass

app = win32.GetActiveObject("Illustrator.Application")
doc = app.activeDocument

saveOpts = EPSSaveOptions(CoClassBaseClass)
saveOpts.EmbedAllFonts = True
saveOpts.SaveMultipleArtboards = True
doc.SaveAs(SaveIn = 'S:/Workstation - DavidK/Code/Main/DeleteLater/tempSheet.eps', Options = saveOpts)

参考文件中难以理解的代码片段

# This CoClass is known by the name 'Illustrator.EPSSaveOptions.24'
class EPSSaveOptions(CoClassBaseClass): # A CoClass
    # Options which may be supplied when saving a document as an Illustrator EPS file
    CLSID = IID('{99D7941A-05F9-419A-BA50-4C6148B54187}')
    coclass_sources = [
    ]
    coclass_interfaces = [
        _EPSSaveOptions,
    ]
    default_interface = _EPSSaveOptions

class _EPSSaveOptions(DispatchBaseClass):
    'Options which may be supplied when saving a document as an Illustrator EPS file'
    CLSID = IID('{95CD20A7-AD72-11D3-B086-0010A4F5C335}')
    coclass_clsid = IID('{99D7941A-05F9-419A-BA50-4C6148B54187}')

    def SetObjectValue(self, arg0=defaultUnnamedArg):
        return self._oleobj_.InvokeTypes(0, LCID, 8, (24, 0), ((9, 0),),arg0
            )

    _prop_map_get_ = {
        # Method 'Application' returns object of type '_Application'
        "Application": (1667330160, 2, (9, 0), (), "Application", '{95CD20AA-AD72-11D3-B086-0010A4F5C335}'),
        "ArtboardRange": (1884304483, 2, (8, 0), (), "ArtboardRange", None),
        "CMYKPostScript": (1883459667, 2, (11, 0), (), "CMYKPostScript", None),
        "Compatibility": (1883849584, 2, (3, 0), (), "Compatibility", None),
        "CompatibleGradientPrinting": (1883457360, 2, (11, 0), (), "CompatibleGradientPrinting", None),
        "EmbedAllFonts": (1883586886, 2, (11, 0), (), "EmbedAllFonts", None),
        "EmbedLinkedFiles": (1883861065, 2, (11, 0), (), "EmbedLinkedFiles", None),
        "FlattenOutput": (1884243564, 2, (3, 0), (), "FlattenOutput", None),
        "IncludeDocumentThumbnails": (1883849588, 2, (11, 0), (), "IncludeDocumentThumbnails", None),
        "JapaneseFileFormat": (1883924070, 2, (11, 0), (), "JapaneseFileFormat", None),
        "Overprint": (1883131728, 2, (3, 0), (), "Overprint", None),
        "PostScript": (1884312428, 2, (3, 0), (), "PostScript", None),
        "Preview": (1634291798, 2, (3, 0), (), "Preview", None),
        "SaveMultipleArtboards": (1397571938, 2, (11, 0), (), "SaveMultipleArtboards", None),
    }
    _prop_map_put_ = {
        "ArtboardRange": ((1884304483, LCID, 4, 0),()),
        "CMYKPostScript": ((1883459667, LCID, 4, 0),()),
        "Compatibility": ((1883849584, LCID, 4, 0),()),
        "CompatibleGradientPrinting": ((1883457360, LCID, 4, 0),()),
        "EmbedAllFonts": ((1883586886, LCID, 4, 0),()),
        "EmbedLinkedFiles": ((1883861065, LCID, 4, 0),()),
        "FlattenOutput": ((1884243564, LCID, 4, 0),()),
        "IncludeDocumentThumbnails": ((1883849588, LCID, 4, 0),()),
        "JapaneseFileFormat": ((1883924070, LCID, 4, 0),()),
        "ObjectValue": ((0, LCID, 4, 0),()),
        "Overprint": ((1883131728, LCID, 4, 0),()),
        "PostScript": ((1884312428, LCID, 4, 0),()),
        "Preview": ((1634291798, LCID, 4, 0),()),
        "SaveMultipleArtboards": ((1397571938, LCID, 4, 0),()),
    }
    # Default method for this class is 'ObjectValue'
    def __call__(self, arg0=defaultUnnamedArg):
        return self._oleobj_.InvokeTypes(0, LCID, 8, (24, 0), ((9, 0),),arg0
            )

    def __unicode__(self, *args):
        try:
            return unicode(self.__call__(*args))
        except pythoncom.com_error:
            return repr(self)
    def __str__(self, *args):
        return str(self.__unicode__(*args))
    def __int__(self, *args):
        return int(self.__call__(*args))
    def __iter__(self):
        "Return a Python iterator for this object"
        try:
            ob = self._oleobj_.InvokeTypes(-4,LCID,3,(13, 10),())
        except pythoncom.error:
            raise TypeError("This object does not support enumeration")
        return win32com.client.util.Iterator(ob, None)

复现问题需使用第三方Illustrator Python脚本参考文件。

内容的提问来源于stack exchange,提问作者DavidK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:42:02