Copy link to clipboard
Copied
Hello everyone,
I have a PSD file for a Business Card template, and I want to write a code that can get into Excel, replace Text Layers in the PSD file with data from Excel columns, export the result as a PNG file, and save it with the Full Name from Excel, and then iterate this as many rows as there are in the Excel file.
I am trying out with the code below but unfortunately I am getting
Can anyone help me please?
Thank you so much in advance.
Here's the code that I have now:
// Prompt the user to enter the path to the PSD file
var psdFilePath = prompt("Enter the path to the PSD file:");
// Open the PSD file
var docRef = app.open(new File(psdFilePath));
// Prompt the user to enter the path to the Excel sheet
var excelFilePath = prompt("Enter the path to the Excel sheet:");
// Prompt the user to enter the output folder path
var outputFolderPath = prompt("Enter the path to the output folder:");
// Open the Excel sheet
var excelFile = new File(excelFilePath);
var excelApp = new BridgeTalk();
excelApp.target = "excel";
excelApp.body = "var app = new ActiveXObject('Excel.Application'); app.Visible = false; app;";
excelApp.send(30000);
// Define the text layers to be replaced
var textLayers = [ { name: "Full Name", columnName: "Name" }, { name: "Job Title", columnName: "Job Title" }, { name: "Email Address", columnName: "E-Mail" }, { name: "Phone Number", columnName: "Phone Number" }];
// Loop through the Excel sheet rows and replace the text layers with data from the Excel sheet
var sheet = workbook.Sheets("Data");
var lastRow = sheet.Cells(sheet.Rows.Count, 1).End(-4162).Row; // Get last row of data
for (var i = 2; i <= lastRow; i++) { // Start at row 2, assuming first row is header row
var name = sheet.Cells(i, 1).Value;
// Loop through the text layers and replace the text with data from the Excel sheet
for (var j = 0; j < textLayers.length; j++) {
var layer = docRef.layers.getByName(textLayers[j].name);
var columnName = textLayers[j].columnName;
var cellValue = sheet.Cells(i, sheet.Range(columnName + "1").Column).Value;
layer.textItem.contents = cellValue;
}
// Save the resulting image as a PNG file with the name from Excel
var savePath = outputFolderPath + "\\" + name + ".png";
docRef.saveAs(new File(savePath), new PNGSaveOptions());
}
// Close the PSD file and Excel sheet
docRef.close(SaveOptions.DONOTSAVECHANGES);
workbook.close();
excelApp.quit();
Did you get that code from ChatGPT?
I believe that you need to use a standard comma or tab-separated/value text file.
Do you know about the following native method, without scripting?
https://helpx.adobe.com/photoshop/using/creating-data-driven-graphics.html
Copy link to clipboard
Copied
Did you get that code from ChatGPT?
I believe that you need to use a standard comma or tab-separated/value text file.
Do you know about the following native method, without scripting?
https://helpx.adobe.com/photoshop/using/creating-data-driven-graphics.html
Copy link to clipboard
Copied
Yes, I got that code from ChatGPT.
I tried running the code on the same Excel file saved as .csv but unfortunately with no luck.
I came across this native method online but I thought it will cause headache and that doing it via a script will be much better and easier.
However, I might start thinking of this native method if the scripting method could not be achieved.
Copy link to clipboard
Copied
ChatGPT is often a poor source for fully working copy/paste code for non-scripters. 😐
You would need to use different code to get plain text data that's comma or tab delimited.
Copy link to clipboard
Copied
Thank you so much, now I got it working using the native method after converting the Excel to a comma-separated text file as you said.