Search code examples
pythonadobe-indesign

Python and Indesign - how to run the script?


I'm trying a script I found on github to get started with this, and I have an error.

import win32com.client
import os

app = win32com.client.Dispatch('InDesign.Application.CC.2017')

myFile = r'C:\Users\gitte\Documents\Gittel Designs\eMAIL BANNERS\EmailBanners.indd'
myDocument = app.Open(myFile)

idPDFType = 1952403524
if app.Documents.Count is not 0:
    directory = os.path.dirname(myFile)
    docBaseName = myDocument.Name
    for x in range(0, myDocument.Pages.Count):
        myPageName = myDocument.Pages.Item(x + 1).Name
        # We want to export only one page at the time
        app.PDFExportPreferences.PageRange = myPageName
        # strip last 5 char(.indd) from docBaseName
        myFilePath = directory + "\\" + docBaseName[:-5] + "_" + myPageName + ".pdf"
        myDocument.Export(idPDFType, myFilePath)

myDocument.Close()

This is my error, which I assume has to do with Indesign - I've tried changing the year, but how can I get it to recognize my application?

runfile('C:/Users/gitte/Documents/SPYDER PYTHON/indesign export all pages as pdfs.py', wdir='C:/Users/gitte/Documents/SPYDER PYTHON')
Traceback (most recent call last):

  File "C:\Users\gitte\Documents\SPYDER PYTHON\indesign export all pages as pdfs.py", line 10, in <module>
    app = win32com.client.Dispatch('InDesign.Application.CC.2017')

  File "C:\Users\gitte\Anaconda3\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)

  File "C:\Users\gitte\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)

  File "C:\Users\gitte\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)

com_error: (-2147221005, 'Invalid class string', None, None)


Solution

  • Your string might not be correct. For 2020 it's "InDesign.Application.2020" the below works on mine. If you're unsure search the registry for "InDesgin.Application". Hope this helps.

    app = win32com.client.Dispatch("InDesign.Application.2020")