• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
10

scripting for CSV to Indd template

Community Beginner ,
Nov 16, 2023 Nov 16, 2023

Copy link to clipboard

Copied

Hi all, I'm having a CSV file which has 4 rows for the respective template in the attached file. But when i try to run the script it is not placing row1 in one frames and row2 in other. Could any suggest whats the best way to do this.. Thank you

PradeepReddy33668770rus6_0-1700165819127.png

 

TOPICS
Scripting

Views

183

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 16, 2023 Nov 16, 2023

Copy link to clipboard

Copied

Hi @Pradeep Reddy33668770rus6, please post the script and a demo .indd file so we can see what's going wrong.

- Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 16, 2023 Nov 16, 2023

Copy link to clipboard

Copied

Below is the code, CSV and indd template. Thank you


var myDocument = app.open(File("C:\\Users\\Prade\\Desktop\\DataMerge\\POC3.indd"));

// Define the data source (CSV file) and open it
var csvFile = File("C:\\Users\\Prade\\Desktop\\DataMerge\\Book2.csv");
if (!csvFile.exists) {
    alert("File does not exist: " + csvFilePath);
    exit();
}

// Open the CSV file
try {
    csvFile.open("r");
    var csvData = csvFile.read();
    csvFile.close();
} catch (e) {
    alert("Error reading the file: " + e.message);
    exit();
}
//csvFile.open("r");
//var csvData = csvFile.read();
//csvFile.close();

// Split the CSV data into an array of rows
var csvRows = csvData.split("\n");

// Loop through the rows and populate the template
for (var i = 1; i < csvRows.length - 1; i++) {
    var rowData = csvRows[i].split(",");
   
    // Extract data from the CSV rows (adjust the indices accordingly)
    var image = rowData[0];
    var Headline = rowData[1];
    var Body = rowData[2];
    var Price = rowData[3];

    // Place the data into the InDesign template (adjust the coordinates and text frames)
    var imageFrame = myDocument.pages[0].rectangles[0];
    var headlineFrame = myDocument.pages[0].textFrames[0];
    var bodyCopyFrame = myDocument.pages[0].textFrames[1];
    var pricingFrame = myDocument.pages[0].textFrames[2];

    imageFrame.place(File(image));
    imageFrame.fit(FitOptions.PROPORTIONALLY);
    imageFrame.fit(FitOptions.CENTER_CONTENT);
    headlineFrame.contents = Headline;
    bodyCopyFrame.contents = Body;
    pricingFrame.contents = Price;

   
    if (i < csvRows.length - 1) {
        myDocument.pages.add();
    }
}

// Export the document to PDF-X1A format
var exportFile = File("C:\\Users\\Prade\\Desktop\\DataMerge\\POC.pdf");
var presetName = "[PDF/X-1a:2001]";
var exportPreset;
if (app.pdfExportPresets.itemByName(presetName).isValid) {
    exportPreset = app.pdfExportPresets.itemByName(presetName);
} else {
   
    alert("Preset not found!");
   
    exportPreset = app.pdfExportPresets.item(0);
}
//var exportPreset = app.pdfExportPresets.itemByName("PDF/X-1a");

myDocument.exportFile(ExportFormat.PDF_TYPE, exportFile, false, exportPreset);

// Close the InDesign document
myDocument.close(SaveOptions.NO);

alert("PDF generation complete!");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 16, 2023 Nov 16, 2023

Copy link to clipboard

Copied

I'm unable to think of a way where i could fill CSV rows into particular template. Please advice if you know ay. Thank you

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 16, 2023 Nov 16, 2023

Copy link to clipboard

Copied

LATEST

Your code is always referring to elements on the 1st page:

 

myDocument.pages[0]

 

When you are adding pages - you should take this into account. 

 

The easiest way would be to index by "-1"

 

myDocument.pages[-1]

 

but then it's not guaranteed that order of the elements will be the same as on the 1st page, so this part of the code may not work on newly added pages:

 

   // Place the data into the InDesign template (adjust the coordinates and text frames)
    var imageFrame = myDocument.pages[0].rectangles[0];
    var headlineFrame = myDocument.pages[0].textFrames[0];
    var bodyCopyFrame = myDocument.pages[0].textFrames[1];
    var pricingFrame = myDocument.pages[0].textFrames[2];
 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines