Text Replacement Script for Illustrator, Text Replace with Csv

Introduction :
For example, it refers to seat cards or tickets. Name badges and so on.
The final output is in PDF format.
need a CSV file where each row in the table will generate a PDF.
need an AI template, where the first row of the CSV will be replaced.
need this JS script that is an Illustrator script.
Other_way

Recently I found another solution.
Although not as simple and intuitive as in this article. But the embeddability of its illustreator is better.


https://www.youtube.com/watch?v=AynG36wTLM0
https://www.marspremedia.com/software/illustrator/find-replace-from-csv

// Set the file paths for the template file and the CSV file
var templateFilePath = “E:/JS/template.ai”;
var csvFilePath = “E:/JS/L.csv”;

// Read the template file
var templateFile = new File(templateFilePath);
var templateDoc;

// Read the content of the CSV file
var csvFile = new File(csvFilePath);
csvFile.open(“r”);
var csvContent = csvFile.read();
csvFile.close();

// Parse the content of the CSV file
var csvLines = csvContent.split(“\n”);
var header = csvLines[0].split(“,”); // Header row of the CSV file
csvLines.shift(); // Remove the header row

// Iterate through each line of the CSV file
for (var i = 0; i < csvLines.length; i++) {
var line = csvLines[i].split(“,”);

// Open the template file
templateDoc = app.open(templateFile);

// Generate a number
var number = 1000 + i;

// Iterate through the header row and replace corresponding text objects

#target illustrator
#Version Alpha 0.1
#OpenAI提供版权声明转载需要咨询律师或合法专业人士,以确保其准确性和合法性。
#建议您咨询专业法律咨询师或律师来帮助您撰写适合您特定情况的版权声明。他们将根据您的需求和要求提供有关版权保护、责任限制和其他法律事项的适当建议和文件。
#请确保在更改或使用脚本或任何其他内容之前,您已经了解并遵守相关的版权法律和规定,以确保您的权益得到保护。

// 设置模板文件路径和CSV文件路径
var templateFilePath = "E:/JS/template.ai";
var csvFilePath = "E:/JS/L.csv";


// 读取模板文件
var templateFile = new File(templateFilePath);
var templateDoc;

// 读取CSV文件内容
var csvFile = new File(csvFilePath);
csvFile.open("r");
var csvContent = csvFile.read();
csvFile.close();

// 解析CSV文件内容
var csvLines = csvContent.split("\n");
var header = csvLines[0].split(","); // CSV文件的标题行
csvLines.shift(); // 删除标题行

// FOR遍历CSV文件的每一行
for (var i = 0; i < csvLines.length; i++) {
  var line = csvLines[i].split(",");

  // 打开模板文件
  templateDoc = app.open(templateFile);

  // 生成编号
  var number = 1000 + i;

  // FOR遍历标题行,逐一替换对应的文本对象
  for (var j = 0; j < header.length; j++) {
    var columnName = header[j];
    var columnValue = line[j];

    // FOR查找包含 columnName 的文本对象并替换内容
    var textItems = templateDoc.textFrames;
    for (var k = 0; k < textItems.length; k++) {
      var textItem = textItems[k];
      if (textItem.contents.indexOf(columnName) !== -1) {
        textItem.contents = textItem.contents.split(columnName).join(columnValue);
      }
    }
  }

  // 导出为PDF文件
  var pdfFilePath = "E:/JS/" + (i + 1) + ".pdf";
  var pdfSaveOptions = new PDFSaveOptions();
  templateDoc.saveAs(new File(pdfFilePath), pdfSaveOptions);

  // 关闭lin模板文件
  templateDoc.close();
}
The script above requires customization.
Please provide the following annotations in English for me:
1.Set the file paths for the template file and the CSV file.
2.Read the template file.
3.Read the content of the CSV file.
4.Parse the content of the CSV file.
5.Iterate through each line of the CSV file.
6.Open the template file.
7.Generate a number.
8.Iterate through the header row and replace corresponding text objects.
9.Find text objects containing columnName and replace their content.
10.Export as a PDF file.
11.Close the template file.
1
0