pythonpsd

Change text layer using psd-tools


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.


Solution

  • 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')