使用Python获取pptx文件的幻灯片标题

时间:2016-11-26 13:15:30

标签: python powerpoint python-pptx

我正在尝试使用Python获取powerpoint文件的每张幻灯片的标题。我在Python中使用Presentation包但我找不到任何指定标题的东西。 我有这个代码返回powerpoint文件的内容。但我需要指定标题。

from pptx import Presentation

prs = Presentation("pp.pptx")

# text_runs will be populated with a list of strings,
# one for each text run in presentation
text_runs = []

for slide in prs.slides:
    for shape in slide.shapes:
        if not shape.has_text_frame:
            continue
        for paragraph in shape.text_frame.paragraphs:
            for run in paragraph.runs:
                text_runs.append(run.text)

2 个答案:

答案 0 :(得分:3)

这是我的解决方案:

from pptx import Presentation

filename = path_of_pptx

prs = Presentation(filename)

for slide in prs.slides:
    title = slide.shapes.title.text
    print(title)

输入:

enter image description here

输出:

Hello, World!
Hello, World2!
Hello, World3!

答案 1 :(得分:0)

正如@scanny指出的那样,要以@eyllanesc的答案为基础,slide.shapes.title是一个占位符。

这意味着您可以访问标题文本,如:

from pptx import Presentation

prs = Presentation(ppt_filename)

slide = prs.slides[0]
slide.shapes.title.text = 'New Title'
print('New Title is:')
print(slide.shapes.title.text)

并更改其他任何标题占位符属性,例如:

slide.shapes.title.top = 100
slide.shapes.title.left = 100
slide.shapes.title.height = 200