简体   繁体   English

使用 Python-pptx 居中对齐文本

[英]Center-align text with Python-pptx

Question in short: Is it possible to align text to the center in Python-pptx?简而言之:是否可以在 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!由于我使用的是 Python-pptx,我已经能够自动化很多事情,我真的很喜欢使用它! However, I've run into a problem.但是,我遇到了一个问题。 I'm trying to center my text horizontally on a slide.我正在尝试将我的文本水平居中放置在幻灯片上。 If you don't understand me:如果你不明白我的意思:

看看前两段与其他两段有何不同?

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.我读了一些关于“MSO_VERTICAL_ANCHOR”和“PP_PARAGRAPH_ALIGNMENT”的东西,但我就是无法让它工作。

Thank you in advance!先感谢您!

from pptx.enum.text import PP_ALIGN

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

This is taken directly from the Python pptx Docs .这是直接取自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.您在问题中说您听说过PP_PARAGRAPH_ALIGNMENT但无法使其正常工作。 What problems are arising?出现了哪些问题?

You can view more information regarding Python pptx alignment here .您可以在此处查看有关 Python pptx 对齐的更多信息。

Scanny, who commented below me added a wonderful point that will solve your problem: Scanny,在我下面发表评论添加了一个可以解决您问题的精彩观点:

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.在您问题中包含的代码中,如果您添加一行p.alignment = PP_ALIGN.CENTER您应该得到您所追求的。

Based on the answer by @scanny and @Harrison I got the following code working with the two methods:根据@scanny 和@Harrison 的回答,我得到了以下代码,可以使用这两种方法:

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')

What worked for me is using PP_PARAGRAPH_ALIGNMENT对我有用的是使用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 在某个 text_frame 的段落之间切换:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM