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

Please try this...

import win32com.client

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

# Remember current unit settings and then set units to

# the value expected by this script

originalRulerUnits = psApp.Preferences.RulerUnits

psApp.Preferences.RulerUnits = 1 # 1= psPixesl, 2 = inches

docs = psApp.Documents

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

# Create a new art layer containing text

layers = psApp.activeDocument.artLayers

artLayerRef = layers.add

artLayerRef.kind = 2 #Text layer

# Set the contents of the text layer.

textItemRef = artLayerRef.TextItem

textItemRef.size = 72

textItemRef.Contents = "Hello People!"

# Restore unit setting

psApp.Preferences.RulerUnits = originalRulerUnits


im still getting the same error.

Known Participant
July 28, 2018

layerRef.Kind = 1 works..

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