Reputation: 51
I'm new in work with python pptx and I need to alignment the title text. I was written this code bellow code:
from PIL import Image
from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
prs.slide_width = Inches(16)
prs.slide_height = Inches(9)
title = slide.shapes.title
title.text="KBS User Activity"
But as you can see in the picture the title is not aligned on the left. Do you have a solution for this?
Upvotes: 2
Views: 1666
Reputation: 19
I used this code, it worked for me. Try it.
from pptx import Presentation
from pptx.enum.text import PP_ALIGN
prs = Presentation()
layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(layout)
#Title name for the slide
title1 = slide.shapes.title
title1.text = "KBS User Activity"
title1.text_frame.paragraphs[0].alignment = PP_ALIGN.LEFT
Upvotes: 1
Reputation: 28893
The title of a slide (slide.shapes.title
) is a placeholder shape. The characteristics of the text (position, text-size, alignment, etc.) placed into that shape with title.text
is determined by the placeholder. The title placeholder on the slide is "cloned" from the title placeholder on the slide-layout it is created from.
So if you change the formatting of the title placeholder on the slide-layout you create the slide from, those changes will be reflected on that slide and any others you create from that layout.
In this case, you are using the default template built into python-pptx
. That template is a 4 x 3 template which is why the title looks somewhat oddly placed when displayed in 16 x 9 (wide, HD) size. Because that template is built-in, you cannot readily change the slide layouts it contains.
The solution is to use your own starting template like:
prs = Presentation("my-blank-presentation.pptx")
Then you can use PowerPoint to adjust those slide layouts to suit, including the paragraph alignment and placeholder position.
To create such a template just create a PowerPoint presentation that looks the way you want, delete all the slides it contains (but not the slide master or slide layouts) and save it as a convenient name like my-blank-presentation.pptx
.
Upvotes: 3