使用Photoshop-Python-API操作PSD报错:请检查Photoshop安装正确性
我编写了Python代码读取Excel数据并填充到PSD指定图层,但运行时抛出错误:photoshop.api.errors.PhotoshopPythonAPIError: Please check if you have Photoshop installed correctly.
已尝试提前启动Photoshop、将Photoshop路径加入系统环境变量,问题仍未解决。
代码如下:
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 COM组件注册正常
找到Photoshop安装目录(如C:\Program Files\Adobe\Adobe Photoshop 202X),以管理员身份打开命令提示符,进入该目录后执行:Photoshop.exe /regserver执行完成后重启电脑,再运行脚本。
检查Python与Photoshop位数匹配
确保Python是64位版本(Photoshop当前均为64位),打开Python终端输入以下命令查看位数:import platform; print(platform.architecture())若为32位Python,需卸载后安装64位版本。
手动指定Photoshop路径到Session
修改代码中Session的初始化语句,明确指定Photoshop.exe的路径:with Session( psd_template_path, action="open", path=r"C:\Program Files\Adobe\Adobe Photoshop 2024\Photoshop.exe" ) as ps:替换路径为你实际的Photoshop安装路径。
开启Photoshop远程连接权限
打开Photoshop,依次进入编辑 > 首选项 > 增效工具,勾选允许远程连接,保持默认端口或设置密码(若设密码,需在Session中添加password="你的密码"参数),重启Photoshop。重新安装Photoshop Python API
卸载现有API后重新安装,避免缓存问题:pip uninstall photoshop-python-api pip install photoshop-python-api --no-cache-dir检查系统权限
以管理员身份运行Python脚本,确保当前用户拥有Photoshop的执行权限。
内容的提问来源于stack exchange,提问作者RandomnieBukvi

