Search code examples
pythondocxpython-docx

Python: issue using WD_ALIGN_PARAGRAPH.RIGHT with photo


When using the titular command with an image after using "add_run", the image is not right aligned. I was attempting to recreate something similar to this but swapping the picture and text section. Unsure if this can be achieved with the library.

from docx import Document
from docx2pdf import convert
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Inches, RGBColor, Pt

header = doc1.sections[0].header
htable = header.add_table(1,2,Inches(9))
htab_cells = htable.rows[0].cells
ht0=htab_cells[1].add_paragraph()
kh=ht0.add_run()
kh.add_picture(holder_logo,width=Inches(.75))
kh.alignment = WD_ALIGN_PARAGRAPH.RIGHT
header1=htab_cells[0].add_paragraph().add_run("Notice to Zero Dollar Change\nDATA CENTER")
header1.font.size = Pt(16)
header1.font.name = 'AvantGarde LT Book'
header1.font.color.rgb = RGBColor.from_string("0266BB")

Solution

  • All I had to do was to swap the line

    kh.alignment = WD_ALIGN_PARAGRAPH.RIGHT
    

    to

    ht0.alignment = WD_ALIGN_PARAGRAPH.RIGHT
    

    Unsure as to why but that did the trick now the logo is oriented properly.