Skip to content Skip to sidebar Skip to footer

Python Indesign Script _ How To Insert A Picture Into A Page And Autofit The Size?

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.Rectangl

Solution 1:

To place an image into a frame you need to fix the last line of your code this way:

myRectangle.Place(img_path)

The place() method accepts a path string, not a mathplot object.

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

or

myRectangle.frameFittingOptions.fittingOnEmptyFrame = 1668247152# PROPORTIONALLY

or

myRectangle.frameFittingOptions.fittingOnEmptyFrame = 1718185072# FILL_PROPORTIONALLY

etc. It depends on what do you want to gain.


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)

I wonder why the greatest master @Jongware didn't managed to answer this?

Post a Comment for "Python Indesign Script _ How To Insert A Picture Into A Page And Autofit The Size?"