如何用Python pptx替换PPP中的Text?

问题描述 投票:0回答:3

我想用Python-pptx替换Powerpoint文本框中的文本。我在网上发现的一切都不适合我,文档对我没有帮助。

所以我有一个带文本的文本框:

  • $$Name 1$$
  • $$Name 2$$

我想将$$Name1 $$改为Tom

我怎样才能做到这一点?

python powerpoint python-pptx
3个回答
0
投票

python-pptx中定义的TextFrame对象有助于操作文本框的内容。你可以这样做:

from python-pptx import Presentation
"""open file"""
prs = Presentaion('pptfile.pptx')
"""get to the required slide"""
slide = prs.slides[0]
"""Find required text box"""
for shape in slide.shapes:
    if not shape.has_text_frame:
        continue
    text_frame = shape.text_frame
    if "Name 1" == text_frame.text:
        text_frame.text = "Tom"
"""save the file"""
prs.save("pptfile.pptx")

0
投票

试试这个:

import pptx

input_pptx = "Input File Path"
prs = pptx.Presentation((input_pptx))
testString = "$$Name1 $$" 
replaceString = 'Tom'

title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)

for slide in prs.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            if(shape.text.find(testString))!=-1:
                shape.text = shape.text.replace(testString, replaceString)

        if not shape.has_table:
            continue    

prs.save('C:/test.pptx')

0
投票

好的谢谢。我刚刚发现,我的PowerPoint示例完全搞砸了。新的PowerPoint空白后,一切正常

© www.soinside.com 2019 - 2024. All rights reserved.