【问题标题】:InDesign python Script for search and replace用于搜索和替换的 InDesign python 脚本
【发布时间】:2019-01-29 15:45:30
【问题描述】:

我想使用 python 脚本来查找和替换 InDesign 文件中的一些文本,然后将其保存为 pdf。

我设法使用 python 打开 indesign 并将其保存为 pdf,但是我不知道如何搜索文本并将其替换为脚本第一部分生成的随机字符串。

这是我目前得到的:

import win32com.client
import random
import string

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

voucher=id_generator()

app = win32com.client.Dispatch('InDesign.Application.CC.2018')
myFile = r'C:\Users\some_file.indd'
    myDocument = app.Open(myFile)
myPDFFile = r'C:\Users\some_file.pdf'
    directory = os.path.dirname(myPDFFile)

    idPDFType = 1952403524
    # 1=[High Quality Print], 2=[PDF/X-1a:2001] etc..
    myPDFPreset = app.PDFExportPresets.Item(1)
    try:
        if not os.path.exists(directory):
            os.makedirs(directory)
        if os.path.exists(directory):
            myDocument.Export(idPDFType, myPDFFile, False, myPDFPreset)
    except Exception as e:
        print('Export to PDF failed: ' + str(e))
    myDocument.Close()

【问题讨论】:

    标签: python replace adobe-indesign


    【解决方案1】:

    您需要遍历文档的所有 TextFrame,然后使用 ChangeText 函数搜索并替换文本。

    这里是你可以做的一个 sn-p:

    voucher = id_generator()
    searchText = 'test'
    
    app = win32com.client.Dispatch('InDesign.Application.CC.2018')
    app.scriptPreferences.userInteractionLevel = 1699640946
    myFile = r'C:\Users\some_file.indd'
    myDocument = app.Open(myFile)
    myPage = myDocument.Pages.Item(1)
    
    idNothing = 1851876449  #from enum idNothingEnum, see doc_reference
    
    for it in myDocument.TextFrames:
        if searchText in (it.Contents):
            app.FindTextPreferences.FindWhat = searchText
            app.ChangeTextPreferences.ChangeTo = voucher
            it.ChangeText()
            continue        
    
        app.FindTextPreferences.FindWhat = idNothing
        app.ChangeTextPreferences.ChangeTo = idNothing
    
    #and then save the changes as PDF...
    
    

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2018-09-13
      • 2011-02-17
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多