Change text of text layer using psd-tools

Question:

I need to change content of text layer in my PSD file. Here is my code:

from psd_tools import PSDImage

psd = PSDImage.load("raw.psd")
number_layer = list(filter(lambda layer: layer.name == "some cool layer", psd.descendants()))[0]

print(number_layer.text)
number_layer.text = "Smth"

When i tries to change text of layer, it throws
AttributeError: property 'text' of 'TypeLayer' object has no setter. Why? And how can i change text of my layer?

Tried to search all google, but didn’t find anything useful

Asked By: sa1lormoon

||

Answers:

As I searched through the source code of psd-tools, I found this line that says:

Currently, textual information is read-only.

So that means you can’t change the text but only read it. That justifies the error AttributeError: property 'text' of 'TypeLayer' object has no setter.

Same at this line a little below the first one:

@property
def text(self):
    """
    Text in the layer. Read-only.

    .. note:: New-line character in Photoshop is `'\\r'`.
    """
    return self._data.text_data.get(b'Txt ').value.rstrip('x00')
Answered By: LoukasPap

Well, as LoukasPap said, it is impossible to change a text layer using psd-tools. I found new way to change it without psd-tools.

Here is my code:

app = win32com.client.Dispatch("Photoshop.Application")
psd_api = app.Open("Enter here full path to PSD")
layer = psd_api.Layers["Path"].Layers["To"].Layers["Your"].Layers["Layer"]
layer.TextItem.Contents = "Some useful text"

psd_api.Save()
psd_api.Close()

Note that you need to install pywin32

pip install pywin32

Answered By: sa1lormoon
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.