Copy link to clipboard
Copied
Hi,
I am trying to change the Font of the contents of a TextFrame, however when doing so, Illustrator crashes.
I am fairly sure the code is correct, as it correctly replaces the *text* of the TextFrame, just not it's Font.
Below is the snippit in question:
# Find and modify the text object
text_modified = False
for item in target_layer.PageItems:
if item.typename == "TextFrame": # Check if the item is a text object
# Update the text content
item.TextRange.Contents = new_text_content
print(f"Successfully changed Text to '{new_text_content}'")
# Change the font of the text object
try:
# Get the exact font name (ensure this is the correct name from the list)
desired_font = app.TextFonts[new_font_name]
# Apply the font to the entire text range
item.TextRange.CharacterAttributes.TextFont = desired_font
print(f"Font '{new_font_name}' successfully applied!")
except Exception as e:
print(f"Font '{new_font_name}' not found or not applicable: {e}")
text_modified = True
break
new_text_content="Abraham Lincoln",
new_font_name="TimesNewRomanPS-BoldMT", # Use the full font name as Illustrator recognizes it
I have tried various fonts, and when they are spelled and formatted correctly, IE "VerdanaPro-CondBlack" Illustrator will always crash, however if I mis-spell the font or give an incomplete name, IE "Verdana" the script will fail to find it and continue without crashing.
Can anyone suggest how to correctly apply a specified font to a TextFrame? Is there an obvious error I am overlooking?
Your time is appreciated.
Hi,
Thanks for the suggestion. What I ended up doing was recording Macros within Illustrator itself, and then calling them in my Python Script via ExtendScript, like below:
extendscript_code = f"""
(function() {{
var doc = app.activeDocument; // Get the active document
var textFrame;
// Find the TextFrame with the given index
if (doc.textFrames.length > {i}) {{
...
Copy link to clipboard
Copied
As an additional note, this is how I have gotten the exact names of the fonts as recognized by Illustrator:
import win32com.client
# Connect to Illustrator
app = win32com.client.Dispatch("Illustrator.Application")
# List all available fonts
print("Available Fonts in Illustrator:")
for font in app.TextFonts:
print(font.Name)
This will output a list of fonts like such:
CascadiaCode-BoldItalic
CascadiaCode-ExtraLightItalic
CascadiaCode-Italic
CascadiaCode-LightItalic
Copy link to clipboard
Copied
@Tranquil_expert6753 , I don't know if you might need this..
textFrame.font = "TimesNewRomanPS-BoldMT";
textFrame.size = 12;
Copy link to clipboard
Copied
in Python I don't know but just noticed that and drop the ; eol designators from JavaScript. Maybe just write python that writes JavaScript.
Copy link to clipboard
Copied
Hi,
Thanks for the suggestion. What I ended up doing was recording Macros within Illustrator itself, and then calling them in my Python Script via ExtendScript, like below:
extendscript_code = f"""
(function() {{
var doc = app.activeDocument; // Get the active document
var textFrame;
// Find the TextFrame with the given index
if (doc.textFrames.length > {i}) {{
textFrame = doc.textFrames[{i}]; // Target the specific TextFrame
textFrame.selected = true; // Select it
}} else {{
alert("No TextFrame found with index {i}!");
return;
}}
// Trigger the action
var actionName = "Change Font to Thunderstorm"; // Replace with your action name
var actionSetName = "Font Change"; // Replace with your action set name
try {{
app.doScript(actionName, actionSetName);
}} catch (e) {{
alert("Error: " + e.message);
}}
}})();
"""
This has proved to be extremely effective, and has the bonus that I can make sure that all the CharacterAttributes such as kerning, size, etc are matched consistently across all my needs for this script.
Wasn't able to get it working any other way. Hope this helps someone else having a similar issue!
Copy link to clipboard
Copied
I see you got it an easier way. I think TextFonts is Read Only and to make and set the text font you can do this for future reference:
var textFrame = app.activeDocument.textFrames.add(); //make text frame
textFrame.contents = "Some String"; // or use var
textFrame.font = "Thunderstorm"; // sets the font
textFrame.size = 12; //points size of font
textFrame.position = [x, y]; // COORDINATES
textFrame.characterAttributes.color = [0, 0, 0]; //rgb black