Skip to main content
Luca_Scarpellini
Participant
November 24, 2020
Answered

Auto data merge several csv files and save pdf with a specific preset

  • November 24, 2020
  • 1 reply
  • 1703 views

Hello, I need to merge data from a lot of csv files and export every file with a specific pdf preset.

 

I have:

• A folder named "Csv" with all the csv files
• An indesign file named "Template.indd" ready for data merge and linked to the first csv file inside the folder.

• A folder named "Print files" for pdf exports

• A folder named "Union files" for inDesign merged documents

 

I need to:

1. update data (with the Template.indd file for data merge already opened)

2. create the data merged document

3. export the pdf file using the pdf export preset named "calendarmerge" in the "Print files" folder using the csv name for the file

4. save the inDesign file into the "Union files" folder using the csv name for the file

5. close merged document

6. update data source on the main file using the next csv file and repeat the process for every csv file in the folder

 

I've found this script that should work but i need to add some lines to adapt it to my needs.
Could someone help me, please?

 

// templateFile path

var myTemplate = File ("~/Desktop/_templates/Template_01.indt");

app.open(myTemplate);  

// OutputFolder path

var myOutputfolder = Folder ("~/Desktop/_folderOut/");  

// TxtFolder path

var myTxtFolder = Folder ( "~/Desktop/_txt/" );

    

    if ( myTxtFolder != null ) {

    var myTxtFiles = [];

    var myAllTxtFilesList = myTxtFolder.getFiles();

            for (var f = 0; f < myAllTxtFilesList.length; f++) { 

            var myFile = myAllTxtFilesList;

            if (myFile instanceof File && myFile.name.match(/\.txt$/i)) { 

                myTxtFiles.push(myFile);

            

            // merge and record data    

            app.activeDocument.dataMergeProperties.selectDataSource(myFile); 

            app.activeDocument.dataMergeProperties.mergeRecords();

            

    // set the OutputFolder and fileExtension  to ".indd" 

    var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.txt$/i, ".indd"))); 

              

    // save the file  

    app.activeDocument.save(mySavedDocument);

    // close the file  

    app.activeDocument.close();

  }

  }

}

 

This topic has been closed for replies.
Correct answer Mike Bro

Hello,

 

The script ran for me without the dialog popping up asking to save...

Try changing the the last line to:

//Change this line
// close the file  
app.activeDocument.close(); 


//NO if you don't want to save changes
 app.activeDocument.close(SaveOptions.NO);

//YES if you do want to save changes
 app.activeDocument.close(SaveOptions.YES);

 

Regards,

Mike


 

// templateFile path
var myTemplate = File ("~/Desktop/Csv/Template.indd");

app.open(myTemplate);  

// OutputFolder path

var myOutputfolder = Folder ("~/Desktop/Union files/");    

// TxtFolder path

var myTxtFolder = Folder ( "~/Desktop/Csv/" );

    

    if ( myTxtFolder != null ) {

    var myTxtFiles = [];

    var myAllTxtFilesList = myTxtFolder.getFiles();

            for (var f = 0; f < myAllTxtFilesList.length; f++) { 

            var myFile = myAllTxtFilesList[f];

            if (myFile instanceof File && myFile.name.match(/\.csv$/i)) { 

                myTxtFiles.push(myFile);

            // merge and record data    

            app.activeDocument.dataMergeProperties.selectDataSource(myFile); 

            app.activeDocument.dataMergeProperties.mergeRecords();

            

    // set the OutputFolder and fileExtension  to ".indd" 

    var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.csv$/i, ".indd"))); 

              
    // save the file  

    app.activeDocument.save(mySavedDocument);


    export_preset = app.pdfExportPresets.itemByName("calendarmerge");

    var fileName = app.activeDocument.name.replace(/.indd$/i, "");

    var myPDFfolder = Folder ("~/Desktop/Print files/");
    
    app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES

    app.activeDocument.exportFile(ExportFormat.PDF_TYPE, File(myPDFfolder +'/'+ fileName + ".pdf"), false, export_preset);


    app.activeDocument.save(mySavedDocument);


    // close the file  

    app.activeDocument.close();

  }

  }

}
alert("Done!")

 

1 reply

Legend
November 24, 2020

Hello CNM Comunicazione,

 

I'm guessing that your Csv folder contains .csv files not .txt files correct?

You can give the below code a try to see if it works, as I did NOT test it myself.

 

 

 

// templateFile path

var myTemplate = File ("~/Desktop/Csv/Template.indd");

app.open(myTemplate);  

// OutputFolder path

var myOutputfolder = Folder ("~/Desktop/Union files/");  

// TxtFolder path

var myTxtFolder = Folder ( "~/Desktop/Csv/" );

    

    if ( myTxtFolder != null ) {

    var myTxtFiles = [];

    var myAllTxtFilesList = myTxtFolder.getFiles();

            for (var f = 0; f < myAllTxtFilesList.length; f++) { 

            var myFile = myAllTxtFilesList[f];

            if (myFile instanceof File && myFile.name.match(/\.csv$/i)) { 

                myTxtFiles.push(myFile);

            // merge and record data    

            app.activeDocument.dataMergeProperties.selectDataSource(myFile); 

            app.activeDocument.dataMergeProperties.mergeRecords();

            

    // set the OutputFolder and fileExtension  to ".indd" 

    var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.csv$/i, ".indd"))); 

              
    // save the file  

    app.activeDocument.save(mySavedDocument);


    export_preset = app.pdfExportPresets.itemByName("calendarmerge");

    var fileName = app.activeDocument.name.replace(/.indd$/i, "");

    var myPDFfolder = Folder ("~/Desktop/Print files/");
    
    app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES

    app.activeDocument.exportFile(ExportFormat.PDF_TYPE, File(myPDFfolder +'/'+ fileName + ".pdf"), false, export_preset);


    // close the file  

    app.activeDocument.close();

  }

  }

}
alert("Done!")

 

 

 

Regards,

Mike

Luca_Scarpellini
Participant
November 27, 2020

I've just a problem: the scrpt works, but inDesign asks me to save/not save every single union file.
It seems to save the file correctly into the unione files folder, but it asks me to save it again after the pdf export.

Is there any way to avoid this?