Skip to main content
Participating Frequently
February 21, 2024
Answered

Ajout d'image automatique dans tableau

  • February 21, 2024
  • 2 replies
  • 6745 views

Bonjour à tous,

Je rencontre une problématique et je ne sais pas comment faire.

J'ai un tableau de 1000 lignes et dans chaque ligne je dois intégrer une ou plusieurs images (il s'agit de produits chimqiues et ce sont les images de pictogramme de sécuirté), les images varient en fonction du produit donc est différent par ligne. Existe t'il une possibilité pour que par exemple je mette des numéros associés à chaque image, que j'intègre les numéros dans mon tableau et que ca me charge automtiquement les images correspondantes ?

Merci d'avance pour vos retour.

This topic has been closed for replies.
Correct answer danaken3

The following script will replace each file name with an anchored image. Before running it, you will need to:

  1. Add the image names into your table cells. (Separate your file names with a space, or multiple spaces if you want to space out your images more.)

     

  2. Create a paragraph style called "Picto" and apply it to the text you want to replace. (Make sure the paragraph style has center alignment — other than that, it doesn't really matter what the style looks like, just as long as you have it applied to the text so the script knows which text to target).
  3. Create an object style of the same name ("Picto") — use the information in my previous reply to configure that object style.

 

If you are not familiar with installing and running scripts, see this help article.

 

The script works as follows:

  1. It prompts you to select the folder where your images are saved (it will not search through subfolders).

     

  2. Then it searches for any text with the paragraph style "Picto" applied.


  3. Then it replaces the text with an anchored object, with the object style "Picto" applied. (Note there is a typed space between each image.)
  4. If a file cannot be found, an empty frame is inserted and the text will remain. 

 

Let me know if you encounter any issues or need additional guidance!

 

 

 

app.doScript(Main, undefined, undefined, UndoModes.ENTIRE_SCRIPT,"Run Script");
function Main(){
var myDoc = app.activeDocument;
var myFolder = Folder.selectDialog("Select folder to search");

app.findGrepPreferences= NothingEnum.nothing;  
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item("Picto");  
app.findGrepPreferences.findWhat ='\\<(\\w|-)+(\\.)(psd|png|jpg|jpeg|svg|tif|tiff)';  
//edited to add "psd"
var foundTexts = myDoc.findGrep();
var myObjStyle = app.activeDocument.objectStyles.item("Picto");

var i = foundTexts.length;
while (i--) {
    try {
        var ip = foundTexts[i].insertionPoints[0].index;
        var anchoredFrame = foundTexts[i].parent.insertionPoints[ip].rectangles.add();
        anchoredFrame.applyObjectStyle(myObjStyle, true);

        if(File(myFolder + "//" + foundTexts[i].contents).exists) {
            anchoredFrame.place(File(myFolder + "//" + foundTexts[i].contents));
            foundTexts[i].remove();
        }
    }
    catch(err) {
    }
}
app.findGrepPreferences= NothingEnum.nothing;
}

 

 

 

2 replies

danaken3
danaken3Correct answer
Participating Frequently
February 22, 2024

The following script will replace each file name with an anchored image. Before running it, you will need to:

  1. Add the image names into your table cells. (Separate your file names with a space, or multiple spaces if you want to space out your images more.)

     

  2. Create a paragraph style called "Picto" and apply it to the text you want to replace. (Make sure the paragraph style has center alignment — other than that, it doesn't really matter what the style looks like, just as long as you have it applied to the text so the script knows which text to target).
  3. Create an object style of the same name ("Picto") — use the information in my previous reply to configure that object style.

 

If you are not familiar with installing and running scripts, see this help article.

 

The script works as follows:

  1. It prompts you to select the folder where your images are saved (it will not search through subfolders).

     

  2. Then it searches for any text with the paragraph style "Picto" applied.


  3. Then it replaces the text with an anchored object, with the object style "Picto" applied. (Note there is a typed space between each image.)
  4. If a file cannot be found, an empty frame is inserted and the text will remain. 

 

Let me know if you encounter any issues or need additional guidance!

 

 

 

app.doScript(Main, undefined, undefined, UndoModes.ENTIRE_SCRIPT,"Run Script");
function Main(){
var myDoc = app.activeDocument;
var myFolder = Folder.selectDialog("Select folder to search");

app.findGrepPreferences= NothingEnum.nothing;  
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item("Picto");  
app.findGrepPreferences.findWhat ='\\<(\\w|-)+(\\.)(psd|png|jpg|jpeg|svg|tif|tiff)';  
//edited to add "psd"
var foundTexts = myDoc.findGrep();
var myObjStyle = app.activeDocument.objectStyles.item("Picto");

var i = foundTexts.length;
while (i--) {
    try {
        var ip = foundTexts[i].insertionPoints[0].index;
        var anchoredFrame = foundTexts[i].parent.insertionPoints[ip].rectangles.add();
        anchoredFrame.applyObjectStyle(myObjStyle, true);

        if(File(myFolder + "//" + foundTexts[i].contents).exists) {
            anchoredFrame.place(File(myFolder + "//" + foundTexts[i].contents));
            foundTexts[i].remove();
        }
    }
    catch(err) {
    }
}
app.findGrepPreferences= NothingEnum.nothing;
}

 

 

 

daphnéAuthor
Participating Frequently
February 23, 2024

Thank you very much, I've tried it but it doesn't work. II have the block that appears, but it's empty and the text still appears (see the sreenshot). I think i've missed something but can't found what 🙂

I have another question : Does the image has to be in .png only or does it works with .psd image if I add |psd?

Sorry it's the first time for me.

 

Please find the different step that i made and if you have time could you please tell me what i could have done wrong. Thank you

jmlevy
Community Expert
Community Expert
February 23, 2024

J'ai retesté, j'ai ce mesage d'erreur qui s'affiche à présent .. je suis perdue la.


Et la première fois, tu n'avais pas ce message ? Tu as modifié quelque chose dans le script ?

Je vois aussi que tu as indiqué le chemin de ton dossier sur la ligne « var myFolder = Folder.selectDialog ». Ça ne sert à rien, ça sert juste à changer le message qui apparaît en haut de la fenêtre qui te permet de sélectionner le dossier dans lequel se trouvent les images. Ceci dit, je pense que ce n'est pas l'origine du problème.

danaken3
Participating Frequently
February 22, 2024

Would it be easy to add the file names of the images directly into the cell where you want the image(s) to go? Previously I've written a script that could then replace the text with images -- in that case it was in a frame rather than a table, but I think I can adjust my script to work with tables. Can you share a sample screenshot that shows what you want your table to look like?

daphnéAuthor
Participating Frequently
February 22, 2024

Thanks for your answer, please inf join a table. Today, I paste each pictogram on the line, but when I have to move or add a line, the pictograms doesn't move, so it's a long work and I have to re-do the list every 6 months.

 

That woul be awesome if you have a solution.

danaken3
Participating Frequently
February 22, 2024

I think the best long-term solution may be to turn those icons into anchored objects. That way, they will adjust automatically when you insert, remove, or resize lines. 

  1.  In your column with the icons, make sure that the text alignment is centered (even though there will be no text, this will make the anchored objects centered).
  2. Create a cell style for these cells (or update the Table > Cell Options). Under Text, select the appropriate Vertical Justification (top, center, etc.) and adjust the cell insets as needed.
  3.  Create an object style for your icons, with the following options selected:
    Anchored Object Options > Position > Inline.  (Leave Y Offset at 0.) Optionally, you may also select "Prevent Manual Positioning," if you don't want to accidentally move your icons out of place.

     

    —(Optional) Size and Position Options > Size > Adjust: Height & Width (enter your desired dimensions). This may help if you want to make sure all your icons are the same size.
    —(Optional) Frame fitting options > Fitting > Fit Content Proportionally
  4.  Place your icon somewhere on the page (in a separate graphic frame, not in the table itself). Apply the object style you created earlier. When you select the frame, you will see a blue square at top right. To anchor this icon, drag that blue square into the desired cell.

    If you selected the options as outlined above, the icon should slide into the correct position (but let me know if it is not working for you). You should also be able to drag multiple icons into the same cell.

I will still try to figure out a script to help with your initial import of all the icons, but doing the above steps should make it easier to update your file later on.