我正在尝试编写代码,从 Excel 电子表格中获取数据并将其放入 psd 文件中的某些图层中。这是我的代码:
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:\Users\Arsen\Desktop\test.xlsx'
psd_template_path = r'C:\Users\Arsen\Desktop\phtsjjjf.psd'
output_dir = r'C:\Users\Arsen\Desktop'
update_psd_with_excel_data(excel_path, psd_template_path, output_dir)
我的笔记本电脑上有 Photoshop。此代码位于 python 文件中。
我什至无法测试代码的功能,因为当我运行它时它立即给我一个错误:
photoshop.api.errors.PhotoshopPythonAPIError: Please check if you have Photoshop installed correctly.
我尝试先运行photoshop,然后运行代码;我将 Photoshop 位置添加到路径变量中,但它不起作用
尝试以管理员权限运行PS。为我工作