Python docx Lib Center Align image

Ryan picture Ryan · Oct 20, 2014 · Viewed 7.2k times · Source

I'm building a automated report program using https://python-docx.readthedocs.org/en/latest/

I'm trying to center a picture and even tried this trick that I read somewhere on google:

document.add_picture('C:\Users\Public\Pictures\Picture.jpg',height=Inches(3.44)) last_paragraph = document.paragraphs[-1] last_paragraph.style = 'centerstyle'

with no luck...

Anyone out there figured out a way to get around this?

Answer

ivan.zd picture ivan.zd · Feb 5, 2015

You could do something like this.

from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH

my_image = document.add_picture('path/to/image') 
last_paragraph = document.paragraphs[-1] 
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER

I know I am probably replying to late now but maybe it helps to somebody else.