Skip to content Skip to sidebar Skip to footer

Frame Around Inline Images

Is there a way to put frames around inline images with python docx? I have something like: from docx import Document from docx.shared import Mm document = Document() table = doc

Solution 1:

It seems that docx currently has no support for such a feature. Since you are using tables, what you probably could do is the following:

  1. Create new Word template
  2. Define custom table style with the border for a cell where you are going to place your image
  3. Use the template in your Python script with docx like this: document = Document('template.docx')
  4. Apply table style you've just created

Please, read this thread for more details.

Another approach could be less elegant, but 100% working. You just create a border around an image before you use it in docx. You can use PIL (for python2) or Pillow (for pyhton3) module for the image manipulation.

from PIL import Image
from PIL import ImageOps
img = Image.open('img.png')
img_with_border = ImageOps.expand(img, border=1, fill='black')
img_with_border.save('img-with-border.png')

This code will take your img.png file and create a new img-with-border.png outlined with a 1px black border. Just use img-with-border.png in you run.add_picture statement then.

Solution 2:

As mentioned by vrs in comments, the simplest solution is :

table = document.add_table(rows=5, cols=2, style="Table Grid")

and add picture to the "run".

Post a Comment for "Frame Around Inline Images"