使用psd-tools更改文本图层

huangapple go评论147阅读模式
英文:

Change text layer using psd-tools

问题

需要更改PSD文件中文本图层的内容以下是我的代码

```python
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"

当我尝试更改文本图层时,它会抛出AttributeError: 'TypeLayer'对象的属性'text'没有设置器。为什么?我如何更改我的图层的文本?

在Google上搜索了所有内容,但没有找到有用的信息。


<details>
<summary>英文:</summary>

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


```python
from psd_tools import PSDImage

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

print(number_layer.text)
number_layer.text = &quot;Smth&quot;

When I try to change the text layer, it throws
AttributeError: property &#39;text&#39; of &#39;TypeLayer&#39; object has no setter. Why? How can I change text of my layer?

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

答案1

得分: 1

我在浏览psd-tools的源代码时发现了这一行的内容:
> 目前,文本信息是只读的。

这意味着你不能更改文本,只能读取它。这解释了错误AttributeError: &#39;TypeLayer&#39;对象的属性&#39;text&#39;没有setter

同样在这一行稍微在第一行下面一点的地方:

@property
def text(self):
    &quot;&quot;&quot;
    图层中的文本只读

    .. 注意:: Photoshop中的换行符是`&#39;\\\\r&#39;`。
    &quot;&quot;&quot;
    return self._data.text_data.get(b&#39;Txt &#39;).value.rstrip(&#39;\x00&#39;)
英文:

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 &#39;text&#39; of &#39;TypeLayer&#39; object has no setter.

Same at this line a little below the first one:

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

    .. note:: New-line character in Photoshop is `&#39;\\\\r&#39;`.
    &quot;&quot;&quot;
    return self._data.text_data.get(b&#39;Txt &#39;).value.rstrip(&#39;\x00&#39;)

答案2

得分: 1

以下是您要翻译的代码部分:

app = win32com.client.Dispatch("Photoshop.Application")
psd_api = app.Open("输入完整的PSD文件路径")
layer = psd_api.Layers["Path"].Layers["To"].Layers["Your"].Layers["Layer"]
layer.TextItem.Contents = "一些有用的文本"

psd_api.Save()
psd_api.Close()

请注意,您需要安装pywin32库:

pip install pywin32
英文:

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(&quot;Photoshop.Application&quot;)
psd_api = app.Open(&quot;Enter here full path to PSD&quot;)
layer = psd_api.Layers[&quot;Path&quot;].Layers[&quot;To&quot;].Layers[&quot;Your&quot;].Layers[&quot;Layer&quot;]
layer.TextItem.Contents = &quot;Some useful text&quot;

psd_api.Save()
psd_api.Close()

Note that you need to install pywin32

> pip install pywin32

huangapple
  • 本文由 发表于 2023年6月22日 05:12:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定