Change text layer using psd-tools

1k Views Asked by At

I need to change the content of a 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 try to change the text layer, it throws AttributeError: property 'text' of 'TypeLayer' object has no setter. Why? How can I change text of my layer?

Searched all Google, but didn't find anything useful.

2

There are 2 best solutions below

2
LoukasPap On BEST ANSWER

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')
0
sa1lormoon On

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