I’m trying to write code that will take data from an excel spreadsheet and fit it into certain layers in a psd file.Here is my code:
import pandas as pd
from photoshop import Session
import os
def update_psd_with_excel_data(excel_path, psd_template_path, output_dir):
df = pd.read_excel(excel_path)
for index, row in df.iterrows():
brand = row['Brand']
name = row['Name']
price = row['Price']
image_path = row['Image']
with Session(psd_template_path, action="open") as ps:
doc = ps.app.activeDocument
update_text_layer(doc, 'brand', brand)
update_text_layer(doc, 'name', name)
update_text_layer(doc, 'price', price)
update_image_layer(doc, 'product_image', r'%s' % image_path)
output_path = os.path.join(output_dir, f"{name}.jpg")
save_as_jpg(doc, output_path, ps)
def update_text_layer(doc, layer_name, text):
layer = doc.artLayers[layer_name]
if layer.kind == 2: # ps.LayerKind.TextLayer
text_item = layer.textItem
text_item.contents = text
def update_image_layer(doc, layer_name, image_path):
layer = doc.artLayers[layer_name]
if layer.kind == 1: # ps.LayerKind.SmartObjectLayer
layer.smartObject.updateContents(image_path)
def save_as_jpg(doc, output_path, ps, quality=12):
jpg_options = ps.JPEGSaveOptions()
jpg_options.quality = quality
jpg_file = ps.File(output_path)
doc.saveAs(jpg_file, jpg_options, asCopy=True)
excel_path = r'C:UsersArsenDesktoptest.xlsx'
psd_template_path = r'C:UsersArsenDesktopphtsjjjf.psd'
output_dir = r'C:UsersArsenDesktop'
update_psd_with_excel_data(excel_path, psd_template_path, output_dir)
I have photoshop on my laptop. This code is in a python file.
I can’t even test the functionality of the code, because it immediately gives me an error when I run it:
photoshop.api.errors.PhotoshopPythonAPIError: Please check if you have Photoshop installed correctly.
I tried to run photoshop first, and then the code; I added the photoshop location to the path variable, but it didn’t worked