问题描述
我需要找到一种方法,通过 python 脚本将图片插入到 InDesign 项目中.但是代码不起作用.
I need to find a way to insert pictures into an InDesign project through a python script. But the code doesn't work.
myPage = myDocument.Pages.Item(1)
myRectangle = myPage.Rectangles.Add()
myRectangle.GeometricBounds = [7, 1, 9, 7]
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
img_path = r'C:\...\In-Design Project\AA.jpg'
img = mpimg.imread(img_path)
myRectangle.Place(img) **# Error**
错误是:
com_error: (-2147352567, '发生异常.', (35869, 'C:\Program Files\Adobe\Adobe InDesign 2020\InDesign.exe', '无法从给定的 URI 创建链接资源.', 无, 0, 0), 无)
com_error: (-2147352567, 'Exception occurred.', (35869, 'C:\Program Files\Adobe\Adobe InDesign 2020\InDesign.exe', 'Cannot create the link resource from the given URI.', None, 0, 0), None)
我发现了一些可能有用的功能,如下所示,但仍未找到解决方案.在此处输入图片描述
I found some functions that might be helpful as below but still haven't found a solution.enter image description here
图片是插入到现有框架中还是插入到新框架中都没有关系.我真的需要一个可行的方法来实现它.谢谢.
Whether the picture is inserted into an existed frame or into a new frame doesn't matter. I just really need a feasible way to achieve it. Thanks.
推荐答案
要将图像放入框架中,您需要通过以下方式修复代码的最后一行:
To place an image into a frame you need to fix the last line of your code this way:
myRectangle.Place(img_path)
place()
方法接受路径字符串,而不是 mathplot 对象.
The place()
method accepts a path string, not a mathplot object.
我不知道你所说的自动填充"到底是什么意思,因为有几个选项:http://jongware.mit.edu/idcs6js/pe_EmptyFrameFittingOptions.html
I don't know what exactly you do mean by 'autofill' since there are the several options out there: http://jongware.mit.edu/idcs6js/pe_EmptyFrameFittingOptions.html
您可以在最后一行之前添加如下内容:
You can add before the last line something like this:
myRectangle.frameFittingOptions.fittingOnEmptyFrame = 1668575078 # CONTENT_TO_FRAME
或
myRectangle.frameFittingOptions.fittingOnEmptyFrame = 1668247152 # PROPORTIONALLY
或
myRectangle.frameFittingOptions.fittingOnEmptyFrame = 1718185072 # FILL_PROPORTIONALLY
等等.这取决于你想获得什么.
etc. It depends on what do you want to gain.
这里是完整的可重现 Python 代码,仅供未来读者的教育目的:
Just for educational purposes for future readers here is the full reproducible Python code:
from win32com.client import Dispatch
app = Dispatch('InDesign.Application.CS6')
myDocument = app.ActiveDocument
myPage = myDocument.Pages.Item(1)
myRectangle = myPage.Rectangles.Add()
myRectangle.GeometricBounds = [7, 1, 9, 7]
# see http://jongware.mit.edu/idcs6js/pe_EmptyFrameFittingOptions.html
# myRectangle.frameFittingOptions.fittingOnEmptyFrame = 1668575078 # CONTENT_TO_FRAME
# myRectangle.frameFittingOptions.fittingOnEmptyFrame = 1718185072 # FILL_PROPORTIONALLY
myRectangle.frameFittingOptions.fittingOnEmptyFrame = 1668247152 # PROPORTIONALLY
img_path = r'C:\...\In-Design Project\AA.jpg'
myRectangle.Place(img_path)
我想知道为什么最伟大的大师@Jongware 没有设法回答这个问题?
I wonder why the greatest master @Jongware didn't managed to answer this?
这篇关于Python Indesign 脚本_如何将图片插入页面并自动调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!