Home > Enterprise >  How to replace text in Powerpoint?
How to replace text in Powerpoint?

Time:06-13

This is the code I am using to replace text in powerpoint. First I am extracting text from powerpoint and then storing the translated and original sentences as dictionary.

prs = Presentation('/content/drive/MyDrive/presentation1.pptx')


# To get shapes in your slides
slides = [slide for slide in prs.slides]
shapes = []
for slide in slides:
    for shape in slide.shapes:
        shapes.append(shape)

def replace_text(self, replacements: dict, shapes: List):
    """Takes dict of {match: replacement, ... } and replaces all matches.
    Currently not implemented for charts or graphics.
    """
    for shape in shapes:
        for match, replacement in replacements.items():
            if shape.has_text_frame:
                if (shape.text.find(match)) != -1:
                    text_frame = shape.text_frame
                    for paragraph in text_frame.paragraphs:
                        for run in paragraph.runs:
                            cur_text = run.text
                            new_text = cur_text.replace(str(match), str(replacement))
                            run.text = new_text
            if shape.has_table:
                for row in shape.table.rows:
                    for cell in row.cells:
                        if match in cell.text:
                            new_text = cell.text.replace(match, replacement)
                            cell.text = new_text

replace_text(translation, shapes) 

I get a error

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-97-181cdd92ff8c> in <module>()
      9         shapes.append(shape)
     10 
---> 11 def replace_text(self, replacements: dict, shapes: List):
     12     """Takes dict of {match: replacement, ... } and replaces all matches.
     13     Currently not implemented for charts or graphics.

NameError: name 'List' is not defined

translation is a dictionary

translation = {' Architecture': 'आर्किटेक्चर',
 ' Conclusion': 'निष्कर्ष',
 ' Motivation / Entity Extraction': 'प्रेरणा / इकाई निष्कर्षण',
 ' Recurrent Deep Neural Networks': 'आवर्तक गहरे तंत्रिका नेटवर्क',
 ' Results': 'परिणाम',
 ' Word Embeddings': 'शब्द एम्बेडिंग',
 'Agenda': 'कार्यसूची',
 'Goals': 'लक्ष्य'}

May I know why am I getting this error. What chnages should be done to resolve it. Also can I save the replaced text using prs.save('output.pptx')

New Error



TypeError                                 Traceback (most recent call last)
<ipython-input-104-957db45f970e> in <module>()
     32                             cell.text = new_text
     33 
---> 34 replace_text(translation, shapes)
     35 
     36 prs.save('output.pptx')

TypeError: replace_text() missing 1 required positional argument: 'shapes'

CodePudding user response:

The error you are getting 'NameError: name 'List' is not defined' occurs because 'List' isn't a valid type within python Typing. Since Python 3.9, you'll want to use 'list[type]'

For instance:

def replace_text(self, replacements: dict, shapes: list[str]):

Alternatively, you can use python's typing. However, this is deprecated in newer versions.

from typing import List 
def replace_text(self, replacements: dict, shapes: List[str]):
  • Related