英文:
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("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.
答案1
得分: 1
我在浏览psd-tools
的源代码时发现了这一行的内容:
> 目前,文本信息是只读的。
这意味着你不能更改文本,只能读取它。这解释了错误AttributeError: 'TypeLayer'对象的属性'text'没有setter
。
同样在这一行稍微在第一行下面一点的地方:
@property
def text(self):
"""
图层中的文本。只读。
.. 注意:: Photoshop中的换行符是`'\\\\r'`。
"""
return self._data.text_data.get(b'Txt ').value.rstrip('\x00')
英文:
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')
答案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("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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论