Python InDesign 脚本 : Get overflowing textbox from preflight for automatic resizing

标签 python adobe-indesign

感谢this great answer我能够弄清楚如何使用 Python 和 InDesign 脚本 API 对我的文档运行预检。现在我想自动调整溢出文本框的文本大小,但无法弄清楚如何从 Preflight 对象中检索 TextBox 对象。

我提到了API specification ,但所有属性似乎只产生不唯一定义文本框的字符串,如本例所示:

Errors Found (1):

  Text Frame (R=2)

是否有任何方法可以从预检中检索违规对象,以便稍后对其进行操作?我非常感谢您对此事提供更多意见,因为我陷入了困境!

最佳答案

如果您需要的只是查找并修复溢出错误,我建议使用此解决方案:

这是修复文本溢出错误的简单扩展脚本。它减小了事件文档中所有溢出文本框架的字体大小:

var doc = app.activeDocument;

var frames = doc.textFrames.everyItem().getElements();
var f = frames.length

while(f--) {
    var frame = frames[f];
    if (frame.overflows) resize_font(frame)
}

function resize_font(frame) {
    app.scriptPreferences.enableRedraw = false;
    while (frame.overflows) {
        var texts = frame.parentStory.texts.everyItem().getElements();
        var t = texts.length;
        while(t--) {
            var characters = texts[t].characters.everyItem().getElements();
            var c = characters.length;
            while (c--) characters[c].pointSize = characters[c].pointSize * .99;
        }
    }
    app.scriptPreferences.enableRedraw = true;
}

您可以将其保存在任何文件夹中并通过Python脚本运行它:

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()
errors = process.processResults
print('Errors:', errors)

if errors[:4] != 'None':
    script = r'd:\temp\fix_overset.jsx'  # <-- here is the script to fix overset
    print('Run script', script)
    app.DoScript(script, 1246973031)     # run the jsx script

    # 1246973031 --> ScriptLanguage.JAVASCRIPT
    # https://www.indesignjs.de/extendscriptAPI/indesign-latest/#ScriptLanguage.html

    process = app.PreflightProcesses.Add(doc, profile)
    process.WaitForProcess()
    errors = process.processResults
    print('Errors:', errors)             # it should print 'None'
    if errors[:4] == 'None':
        doc.Save()

doc.Close()

input('\nDone... Press <ENTER> to close the window')

关于Python InDesign 脚本 : Get overflowing textbox from preflight for automatic resizing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70114267/

相关文章:

python - 转置 pandas 中的数据框

python - 如何自动生成nameko微服务的rpc接口(interface)文档?

mysql - 从 XML 导入到 InDesign

text-extraction - 访问 Adob​​e InDesign 文件

用于打开子目录中多个文件的 JavaScript 代码

python - csv 列中的连字符/未知数据导致 int 转换错误

python - 无法在 Jupyter notebook 中导入 opencv 但能够在 Anaconda 的命令行中导入

python - 具有 bool 值和整数的数据框的 Pandas 条件子集

javascript - 在 Adob​​e InDesign CS5 中处理文本框中的文本

javascript - 如何在通过 inDesign 的 createTOC 函数生成后定位目录