9

Question in short: Is it possible to align text to the center in Python-pptx?

Since I'm using Python-pptx, I have been able to automate quite a lot of things and I really enjoy using it! However, I've run into a problem. I'm trying to center my text horizontally on a slide. If you don't understand me:

See how the first two paragraphs differ from the other two?

My text is now aligned to the left, similar to the text in the first two paragraphs. However, I'd like them to be aligned to the center, like those last two paragraphs. This is a snippet of my code:

left = Cm(3)
top = Cm(2.5)
width = Cm(15)
height = Cm(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
p = tf.add_paragraph()
run = p.add_run()
run.text = "Just an example"
font = run.font
font.size = Pt(30)

I looked in the documentation, but couldn't find anything useful. I read something about "MSO_VERTICAL_ANCHOR" and "PP_PARAGRAPH_ALIGNMENT", but I just can't get it working.

Thank you in advance!

PythonPupil1906
  • 125
  • 1
  • 1
  • 8

3 Answers3

10
from pptx.enum.text import PP_ALIGN

shape.paragraphs[0].alignment = PP_ALIGN.CENTER

This is taken directly from the Python pptx Docs. Does this not work for you? You said in your question that you've heard of PP_PARAGRAPH_ALIGNMENT but can't get it working. What problems are arising?

You can view more information regarding Python pptx alignment here.

Scanny, who commented below me added a wonderful point that will solve your problem:

Paragraph alignment (also known as justification) is a property of a paragraph and must be applied individually to each paragraph. In the code you included in your question, if you add a line p.alignment = PP_ALIGN.CENTER you should get what you're after.

Harrison
  • 5,095
  • 7
  • 40
  • 60
  • That's indeed one of the options I tried. I replaced 'shape' with 'tf', which didn't produce any errors, but the text is still not aligned (tried some other possibilities too). Probably it shouldn't be 'tf', but something else that I don't know. Anyway, thank you for your effort! – PythonPupil1906 Aug 13 '16 at 19:02
  • 3
    @PythonPupil1906 - Harrison is quite right. Paragraph alignment (also known as *justification*) is a property of a paragraph and must be applied individually to each paragraph. In the code you included in your question, if you add a line `p.alignment = PP_ALIGN.CENTER` you should get what you're after. Don't forget to accept Harrison's answer as the "right" answer. That's how we get "paid" for answering questions here :) Also, in general, you should include the exact code you tried that doesn't seem to be working. – scanny Aug 13 '16 at 23:55
  • @scanny Sweet! It's finally working! Thanks for your example, scanny, you really helped me out! And you did too, Harrison! Thank you both for your time and effort, and have a nice day! – PythonPupil1906 Aug 14 '16 at 07:42
  • I get Cannot find reference 'PP_ALIGN' in 'text.py' – Europa Jun 11 '23 at 11:02
1

Based on the answer by @scanny and @Harrison I got the following code working with the two methods:

from pptx import Presentation
from pptx.util import Inches, Pt,Cm
from pptx.enum.text import PP_ALIGN


prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

left = Cm(3)
top = Cm(2.5)
width = Cm(15)
height = Cm(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
tf.text = "Hello"
txBox.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER

p = tf.add_paragraph()
p.alignment = PP_ALIGN.CENTER
run = p.add_run()
run.text = "Just an example"
font = run.font


prs.save('test.pptx')
Victor
  • 398
  • 1
  • 2
  • 11
0

What worked for me is using PP_PARAGRAPH_ALIGNMENT

from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
shape.paragraphs[0].alignment = PP_PARAGRAPH_ALIGNMENT.CENTER

Have in mind that you can switch between paragraphs of a certain text_frame by using for:

for paragraph in cell.text_frame.paragraphs:
                paragraph.font.color.rgb = RGBColor(255, 255, 255)
                paragraph.font.size = Pt(9)   
                paragraph.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER