Я хотел запустить это в Adobe InDesign 2024 или более поздней версии. Я видел несколько примеров сценариев Python InDesign: получение переполненного текстового поля из предварительной проверки для автоматического изменения размера
в качестве справки, которая может работать в Adobe InDesign CS 6, но мне нужно работать в Adobe InDesign 2024 Creative Cloud.
Код: Выделить всё
import win32com.client
def create_indesign_document_with_text():
try:
# Initialize COM
print("Initializing COM...")
app = win32com.client.Dispatch('InDesign.Application')
# Create a new InDesign document
print("Creating a new InDesign document...")
doc = app.Documents.Add()
# Define the text content
text_content = ("This is a sample text with some bold and italic words. "
"The quick brown fox jumps over the lazy dog. " * 5)
# Define text attributes
bold_text = "This is bold text."
italic_text = "This is italic text."
# Create a text frame
print("Adding a text frame with sample content...")
page = doc.Pages.Item(1)
text_frame = page.TextFrames.Add()
text_frame.GeometricBounds = ["12.7mm", "12.7mm", "287mm", "200mm"] # Adjust the bounds as needed
# Add the main text content
text_frame.Contents = text_content
# Apply formatting to parts of the text
# Note: InDesign uses 0-based index for characters
text_range = text_frame.Characters
text_range.Item(0).ChangeTextPreferences.appliedFont = app.Fonts.Item("Arial")
text_range.Item(0).ChangeTextPreferences.fontStyle = "Bold"
text_range.Item(0).ChangeTextPreferences.pointSize = 12
# Apply bold formatting to a specific range
bold_start = text_content.find(bold_text)
bold_end = bold_start + len(bold_text)
text_range.ItemByRange(bold_start, bold_end - 1).ChangeTextPreferences.fontStyle = "Bold"
# Apply italic formatting to a specific range
italic_start = text_content.find(italic_text)
italic_end = italic_start + len(italic_text)
text_range.ItemByRange(italic_start, italic_end - 1).ChangeTextPreferences.fontStyle = "Italic"
# Save the InDesign document
save_path = r'INDESIGN_FILE_PATH'
print(f"Saving the InDesign document to {save_path}...")
doc.Save(save_path)
# Call the JSX script
jsx_script_path = r'JSX_FILE_PATH'
print(f"Executing JSX script: {jsx_script_path}...")
app.DoScript(jsx_script_path, 1246973031) # Use ScriptLanguage.JAVASCRIPT
print("Document created and script executed successfully.")
# Close the document
print("Closing the InDesign document without saving changes...")
doc.Close(1) # 1 means save changes
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
create_indesign_document_with_text()
При запуске приведенного выше кода я получаю следующую ошибку:
Код: Выделить всё
Initializing COM...
Creating a new InDesign document...
Adding a text frame with sample content...
An error occurred: (-2147352567, 'Exception occurred.',
(30475, 'INDESIGN EXE PATH', 'The requested member of the collection does not exist.', None, 0, 0), None)
С уважением,
AV
Подробнее здесь: https://stackoverflow.com/questions/787 ... rom-python