0

I am getting an issue in my code that is trying to read a psd file without opening photoshop but psd_tools cannot find the layer I want and gives me an error. How can I fix it. (already checked if there is any layer name typos and capitals)

from psd_tools import PSDImage
import os
def change_text_content_and_save(psd_file_path, layer_name, new_text, output_directory):
 try:
 # Open the PSD file
 psd = PSDImage.open(psd_file_path)
 # Find the text layer by name
 text_layer = None
 for layer in psd:
 if layer.name == layer_name and hasattr(layer, 'text_data'):
 text_layer = layer
 break
 if text_layer:
 # Change the text content
 text_layer.text_data.text = new_text
 print(f"Changed text content of layer '{layer_name}' to: {new_text}")
 # Generate the output filename (PSD format)
 output_file_name = os.path.basename(psd_file_path)
 output_file_path = os.path.join(output_directory, output_file_name)
 # Save the modified PSD
 psd.save(output_file_path)
 print(f"Saved the modified PSD as: {new_text}")
 else:
 print(f"Text layer not found: {layer_name}")
 except Exception as e:
 print(f"An error occurred: {str(e)}")
if __name__ == "__main__":
 psd_file_path = "C:/Text_Files/dalle.psd" # Replace with the path to your PSD file
 layer_name = "emo" # Replace with the name of the text layer you want to change
 new_text = "Mehmet Ali" # Replace with the new text content
 output_directory = "C:/Text_Files/output_projects" # Replace with the desired output directory
 change_text_content_and_save(psd_file_path, layer_name, new_text, output_directory)

GOT A ERROR THAT TELLS LAYER NOT FOUND

Daviid
1,60112 silver badges26 bronze badges
asked Sep 19, 2023 at 8:17
2
  • post full error message Commented Sep 19, 2023 at 8:18
  • Try printing all layer.name, does the file really have any layer, and your required one in particular? Maybe you misspelled the layer name? Commented Sep 19, 2023 at 8:24

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.