Copy link to clipboard
Copied
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)
img = mpimg.imread(img_path)
myRectangle.Place(img) **# Error**
The error is :
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
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.
Copy link to clipboard
Copied
I don't know the first thing about Python scripting for InDesign, but from what I can tell
mpimg.imread(img_path)
returns an array of the image data it read. Rectangle.place() needs a File object to place in its first argument. Not sure how you would return a Javascript File object from Python, but that's where I'd investigate. It could be just as simple as creating one out of your img path. Also looks like you are calling img = mpimg.imread(img_path) twice; once in its own line and once in the line above it at the end. That might also be causing problems.
Copy link to clipboard
Copied
Great thanks. Currently what within Rectangle.place() is not a File object. Do you know how to read in an image as a File object in python? Thanks.
Copy link to clipboard
Copied
Not sure. You could try:
myRectangle.Place('C:\...\In-Design Project\AA.jpg')
You could also open the path:
fileobj = ('C:\...\In-Design Project\AA.jpg','r')
myRectangle.Place(filobj)
Copy link to clipboard
Copied
In
myRectangle.Place(img)
'img' must be a file object. I don't know if that's the case in your script.
Copy link to clipboard
Copied
Thanks Peter! But do you know how to read in an image as a File object in python? Thanks.
Copy link to clipboard
Copied
No, I don't. You'd have to check in the Python documentation. It's probably something like
img = File ('C:\...\In-Design Project\AA.jpg')
or
img = new File ('C:\...\In-Design Project\AA.jpg'i)
Another thing: mind the backslashes. Either escape them ('c:\\dir') or use forward slashes ('c:/dir'). Again, there's lots of Python documentation around.
Copy link to clipboard
Copied
For an example have a look at any property that yields a file.
In the JavaScript binding many properties with the name "fullName" actually return a file, e.g. app.fullName.
For Python point your favorite OLE/COM object viewer to the TLB (type library) of InDesign to find candidates.
As an alternative investigate the app.doScript() method, that way your Python program can invoke JavaScript.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now