我有一个带有文本占位符的幻灯片模板。如何在该特定文本占位符中插入文本并设置其格式?
text_ph1 = slide1.placeholders[14]
text_ph2 = slide1.placeholders[15]
para1 = ['Text for para 1']
para2 = ['Text for para 2']
p = text_ph1.paragraphs[0]
run = p.add_run()
run.text = para1[0]
font = run.font
font.name = 'Calibri'
font.size = Pt(14)
font.bold = False
font.italic = None
AttributeError Traceback (most recent call last)
<ipython-input-205-ce3eb19a0d8b> in <module>
2 text_frame.clear() # not necessary for newly-created shape
3
----> 4 p = text_ph1.paragraphs[0]
5 run = p.add_run()
6 run.text = para1[0]
AttributeError: 'SlidePlaceholder' object has no attribute 'paragraphs'
答案 0 :(得分:0)
希望我理解你的问题。
示例:
from pptx import Presentation
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Text for para 1"
subtitle.text = "Text for para 2"
prs.save('test.pptx')
答案 1 :(得分:0)
占位符是一种形状,与其他任何自动形状(矩形,文本框,圆形等)一样。
要访问形状的文本,请使用其.text_frame
属性检索其TextFrame
对象。 TextFrame
对象具有段落(而不是直接的形状),因此应该可以进行以下操作:
text_ph1 = slide1.placeholders[14]
text_ph1.text_frame.text = "Text for para 1"
如果您想格式化添加的文本,则可以在第一段的第一次运行中找到它:
font = text_ph1.text_frame.paragraphs[0].runs[0].font
font.name = 'Calibri'
font.size = Pt(14)
font.bold = False
font.italic = False