Search code examples
pythonadobe-indesign

Python InDesign scripting: How to run a preflight check?


This guide provides a very good overview of InDesign scripting using Python. I am able to open my document and export it to a PDF, but in addition to that I want to check if any textboxes have some overflowing text. I wanted to accomplish this using some preflight checking, but when I tried to call

myPreflight = app.PreflightProcesses.add(myDocument, myProfile)

I got the error "Missing required parameter 'TargetObject' for method 'Add'.", although according to the documentation I did provide the target object.

I would really appreciate a concrete example on how to check for overflowing text, as I am rather new to this approach. Thank you for your time!


Solution

  • Here is the Python snippet that works for me:

    import win32com.client
    
    app = win32com.client.Dispatch('InDesign.Application.CS6')
    doc = app.Open(r'd:\temp\test.indd')
    
    profile = app.PreflightProfiles.Item('Stackoverflow Profile')
    print('Profile name:', profile.name)
    
    process = app.PreflightProcesses.Add(doc, profile)
    process.WaitForProcess()
    results = process.processResults
    print('Results:', results)
    
    doc.Close()
    
    input('\nDone... Press <ENTER> to close the window')
    

    For my test file, when it has the 'Overset Text' error, it shows the output as follows:

    enter image description here

    If there is no error I'm getting this:

    enter image description here

    Of course you need to change the file name and profile name in the code with your actual file name and profile. And perhaps you need to change CS6 with CC.2020 or whatever you have.

    Just in case, ExtendScript variant of the code (for the already opened document) looks like this:

    var myDocument = app.activeDocument;
    var myProfile = app.preflightProfiles.item('Stackoverflow Profile');
    var myPreflight = app.preflightProcesses.add(myDocument, myProfile);
    myPreflight.waitForProcess();
    var results = (myPreflight.processResults);
    alert(results);