2

While using python-pptx module, I came to know it places shape determining shape's center at given length in left parameter. Suppose I have given shape = slide.shapes.add_textbox(Inches(1), top, width, height) then shape's center point will be placed 1 inches away from left side. What I want to do is, shape's left most side should be placed 1 inches away from slide's left side. Because right now if I add more text it's left most text moves out from the slide. Any way to do so?

Similar question I found at link but there is no answer as question is misunderstood and having not much reputation, I can't comment there.

Thanks

Karan Shah
  • 111
  • 11

1 Answers1

2

The .top and .left attributes of a shape represent the distance from the top, left corner of the slide to the top, left corner of the shape. Your idea that shape position is relative to the shape center is mistaken.

If you have text bleeding out of the left side of your shape, I would start by checking the Paragraph.alignment setting for each paragraph in the shape text-frame:

from pptx.enum.text import PP_ALIGN

for paragraph in shape.text_frame.paragraphs:
    paragraph.alignment = PP_ALIGN.LEFT

If a paragraph is aligned right, the text will grow to the left.

Another factor that can be relevant to this behavior is the TextFrame.word_wrap setting. If word-wrap is turned off, text can bleed outside the horizontal extents of the shape.

shape.text_frame.word_wrap = True

Finally, the TextFrame.auto_size behaviors of a shape can cause relocation of the shape, in particular, when the "Resize shape to fit text" option is selected, one or more of the size or position attributes are changed to comply. Note that this setting can interact with the paragraph alignment setting when choosing which side to "stretch" the shape to fit its text.

from pptx.enum.text import MSO_AUTO_SIZE

shape.text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT

You may need to experiment to find the combination that gives the behavior you want. I would say the safest place to start is this:

from pptx.enum.text import MSO_AUTO_SIZE
from pptx.enum.text import PP_ALIGN

shape = slide.shapes.add_textbox(Inches(1), top, width, height)
text_frame = shape.text_frame
text_frame.text = 'Text I want to appear in text-box'
text_frame.auto_size = MSO_AUTO_SIZE.NONE
text_frame.word_wrap = False
for paragraph in text_frame.paragraphs:
    paragraph.alignment = PP_ALIGN.LEFT

Also note the behavior around auto-size can be subtly different on LibreOffice than it is on Microsoft PowerPoint. That will not be the case in the "safe" option above, but something to keep in mind.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • Thanks a lot. I am glad to know that you still actively reply to all pptx questions till date. I was testing in openoffice on mac which showed some difference leading to ask question here. However after your reply I just checked and it works perfectly fine in windows powerpoint. – Karan Shah Oct 29 '18 at 11:21