Skip to main content
lemc
New Participant
January 23, 2019
Answered

Find and Replace Query extracting from a .CSV or .XLS list

  • January 23, 2019
  • 3 replies
  • 8225 views

Hello,

I'm wondering if there is a way to find and replace multiple text in Indesign using data from.CSV or .XLS file.

The .CSV file contains 2 columns, column 1 contains the texts it needs to find and column the is the text it needs to replace.

I've tried the findbychange script but I don't know how to extract information from the .CSV file.

Example below column one shows the words to find and column two the corresponding words to replace. Basically I have more than 2000 unique words I needed to replace.

wheeltire
helloworld
testagain

Thank you very much for the advice.

This topic has been closed for replies.
Correct answer willcampbell7

It's not clear if you want advice on code, or a working script. I've had the same desire on my to-do list for a while. Most of the needed components I have from other scripts both public and private, so this morning I patched together the basics to accomplish your request. I don't have documentation prepared yet, but can tell you the basics -- the CSV or tab-delimited text requires a header row, the names of which can be specified and default to "find" and "change." You'll see when running the script. And the rest should be obvious. This quick solution is only text for now, not yet GREP, which I had always planned to also include (really my biggest desire for such a script, more so than just text changes; all in good time). It sounded like you needed a solution now so I figured whip up what I can and come back later to add polish and other features. Go here to download the script:

http://www.marspremedia.com/software/indesign/find-change-from-spreadsheet

You'll see the download button. Let me know anything that doesn't work right. My e-mail address is on the script UI.

3 replies

willcampbell7
willcampbell7Correct answer
Brainiac
January 25, 2019

It's not clear if you want advice on code, or a working script. I've had the same desire on my to-do list for a while. Most of the needed components I have from other scripts both public and private, so this morning I patched together the basics to accomplish your request. I don't have documentation prepared yet, but can tell you the basics -- the CSV or tab-delimited text requires a header row, the names of which can be specified and default to "find" and "change." You'll see when running the script. And the rest should be obvious. This quick solution is only text for now, not yet GREP, which I had always planned to also include (really my biggest desire for such a script, more so than just text changes; all in good time). It sounded like you needed a solution now so I figured whip up what I can and come back later to add polish and other features. Go here to download the script:

http://www.marspremedia.com/software/indesign/find-change-from-spreadsheet

You'll see the download button. Let me know anything that doesn't work right. My e-mail address is on the script UI.

William Campbell
lemc
lemcAuthor
New Participant
January 25, 2019

Thanks William, I would like to check the script but the link does not work.

willcampbell7
Brainiac
January 25, 2019

Sorry about that... the link I copied was to my ".test" domain, my internal test server. I have it on my real server now...

https://www.marspremedia.com/software/indesign/find-change-from-spreadsheet

William Campbell
Michel Lemieux
Inspiring
January 24, 2019

Hi lemc,

Personally, I am more partial towards using tab delimited files but to each his own ;-)

Here's my APPLESCRIPT answer, the follwing should do the trick:

property textDelim : ";" -- tab

set valueFile to ((path to desktop folder) as text) & "test.csv"

set myValues to read file valueFile using delimiter {return}

set myFindReplacePairs to {}

repeat with thisLine in myValues

  set end of myFindReplacePairs to my getPair(thisLine)

end repeat

on getPair(aLine)

  set {leftOfTab, rightOfTab} to my getTextItem(aLine, textDelim, 0)

  set leftOfTab to (my getTextItem(leftOfTab, ASCII character 10, 0)) as text

  return {findWhat:leftOfTab, replaceWith:rightOfTab}

end getPair

on getTextItem(thisString, thisDelim, thisItem)

  -- ThisString -> String to look in

  -- ThisDelim -> Text element that delimit the string

  -- ThisItem -> Number of the element to return (0 returns ALL)

  copy the text item delimiters to OldDelims

  set the text item delimiters to thisDelim

  set itemsList to every text item of thisString

 

  if class of thisItem is list then

  set fromItem to item 1 of thisItem

  set toItem to item 2 of thisItem

  set theReturn to (items fromItem thru toItem of itemsList) as text

  else

  if thisItem is not 0 then

  set theReturn to (item thisItem of itemsList) as text

  else

  set theReturn to itemsList -- return every items

  end if

  end if

  set the text item delimiters to OldDelims

  return theReturn

end getTextItem

this script populates the list myFindReplacePairs with records fo your pairs in the form of {findWhat: text, replaceWith: text}.

Depending on how your text file is setup (my Excel separates my columns with semi-colon when I save to comma separated values so I created a property (LINE 1) that lets you specify what is separating your values.

LINE 13 is there to strip potential Line Feed character (\n) from your data.

HTH
Michel

Adobe Expert
January 24, 2019

Hi lemc,

Your problem can be divided into two parts

  • Parsing out the csv
  • Using that parsed data to create a find/change text query

For parsing the csv you can look into a library for JS that can do it. Looking through the forum i found a discussion that you can look to get you started on this. Splitting a CSV with a Regular Expression

For find change query you can use the following code snippet, run the code snippet below in a loop that runs for the number of rows you parsed from the csv

app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing; 

app.findTextPreferences.findWhat = "the value that needs to be searched";  //replace the string in quote with the first value of the parsed row, like row[1][0]

app.changeTextPreferences.changeTo = "the replacement value";  //replace the string in quote with the second value of the parsed row, like row[1][1]

app.activeDocument.changeText(); 

app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;

-Manan