Skip to main content
Known Participant
July 28, 2018
Question

python photoshop scripting creating a text layer

  • July 28, 2018
  • 3 replies
  • 8620 views

Hi i have this on my python script

import win32com.client


psApp = win32com.client.Dispatch("Photoshop.Application")


psApp.Open(r"C:\Users\Kim-DEV\psd_env\psdtest.psd"

doc = psApp.Application.ActiveDocument

layerRef = doc.ArtLayers.Add()

layerRef.Kind = 2

after i run the script i get error on layerRef.Kind = 2

it says that

Traceback (most recent call last):

  File "C:\Users\Kim-DEV\psd_env\psd.py", line 8, in <module>

    layerRef.Kind = 2

  File "C:\Users\Kim-DEV\psd_env\lib\site-packages\win32com\client\dynamic.py", line 565, in __setattr__

    self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Adobe Photoshop', '', None, 0, -2147213322), None)

can anyone tell me how to fix this ?

This topic has been closed for replies.

3 replies

Hal.long
Participant
May 29, 2020

Hey,

 

Maybe you can try to use the python API for photoshop.

 

 https://github.com/loonghao/photoshop-python-api

 

The docs:

https://photoshop-python-api.readthedocs.io/en/master/

 

Cheers,

Kukurykus
Legend
July 28, 2018

You can't set layer kind this way, only check its kind. From empty layers you can create only text layers. Say what kind is 2?

Known Participant
July 28, 2018

i wanted to create text layers. and 2 value is a psTextLayer

Known Participant
July 28, 2018

Tried open an existing file and adding a text layer, no problem.

You must be doing something different.


woah. i got it working now

this is my final code

from win32com.client import Dispatch


psApp = Dispatch("Photoshop.Application")

psApp.Open(r"C:\Users\Kim-DEV\psd_env\test1.psd"

doc = psApp.ActiveDocument

layerRef = doc.ArtLayers.Add()


psTextLayer = 2  # from enum PsLayerKind

layerRef.Kind = psTextLayer


textItem = layerRef.TextItem

textItem.Contents = "HELLO WORLD!"

textItem.Position = (120, 120)

Thank you all for the assistance and help.

Known Participant
July 28, 2018

layerRef.Kind = 1 works..

but 2 above i get already errors same on that one.