How to wrap text around an inserted image in Google Docs using Apps Script?

I’m trying to add an image to my Google Doc using Apps Script. Right now, I can insert the image, but it shows up inline with the text. What I really want is to have the text wrap around the image.

Here’s the code I’m using now:

function insertImage() {
  var doc = DocumentApp.getActiveDocument();
  var imageFile = DriveApp.getFileById('my_image_id');
  var imageBlob = imageFile.getBlob();
  doc.getBody().insertImage(0, imageBlob);
}

This puts the image in the document, but it doesn’t allow the text to flow around it. Is there a way to have the ‘Wrap text’ option enabled automatically through the script? I’d appreciate any suggestions on how to achieve this.

Thanks for your help!

I’ve dealt with this issue before, and there’s a solution that works well. Instead of using insertImage(), you can use insertInlineImage() and then set the layout to WRAP_TEXT.

Here’s a modified version of your code that should do the trick:

function insertImageWithTextWrap() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var imageFile = DriveApp.getFileById('my_image_id');
  var image = body.insertInlineImage(0, imageFile.getBlob());
  image.setLayout(DocumentApp.LayoutType.WRAP_TEXT);
}

This approach inserts the image inline first, then adjusts its layout to wrap text. You might need to tweak the position (the ‘0’ parameter) depending on where you want the image in your document. Hope this helps solve your problem!

hey TomDream42, i’ve run into this too. try using the setLayout method after inserting the image. something like:

image.setLayout(DocumentApp.LayoutType.WRAP_TEXT);

that should make the text wrap around ur image. hope it helps!

I’ve actually tackled this challenge before in one of my projects. While the suggestions above are solid, I found a slightly different approach that gives you more control over the image placement and wrapping.

Try this code snippet:

function insertWrappedImage() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var imageFile = DriveApp.getFileById('my_image_id');
  var image = body.insertImage(0, imageFile.getBlob());
  image.setLayout(DocumentApp.LayoutType.WRAP_TEXT);
  image.setWidth(200); // Adjust width as needed
  image.setLeftOffset(20); // Adjust left offset for positioning
}

This method allows you to set the image width and left offset, giving you finer control over how the text wraps. You might need to play around with the values to get the perfect fit for your document layout. Also, remember that the ‘0’ in insertImage(0, …) determines where the image is inserted - you can change this to place the image exactly where you want it in the document.