0

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'
sha25
  • 23
  • 6
  • Your replace_text has the 'self' parameter. I don't see you using it and that parameter is basically only used within classes(and I don't see this being in a class). Just remove the 'self' parameter and you should be good – Vvamp Jun 13 '22 at 09:34
  • It also occurs to me that if the text is split across, say, 2 runs, it won't be found. Possibly not an issue in your use case. – Martin Packer Jun 15 '22 at 07:25
  • @MartinPacker can't really understand you? – sha25 Jun 15 '22 at 07:42
  • Text in a paragraph can be in multiple runs. For example, if part of it is bolded. If the text string you're searching for spans a run boundary it's a bit more complicated to find it (and to work out how to replace it). – Martin Packer Jun 16 '22 at 07:18
  • @MartinPacker - Good to know. I face exactly the same issue where my search term is present in 2 runs. How do we replace in such case? Any pointers or help please https://stackoverflow.com/questions/73219378/python-pptx-how-to-replace-when-string-is-across-multiple-runs – The Great Aug 03 '22 at 10:02
  • But there is no bold or any format applied to my keyword that is present across multiple runs. Don't know why does this happen – The Great Aug 03 '22 at 10:22
  • I think you have to concatenate runs pairwise - to do the search. But think about what to do if the word DOES span two runs. Which run's attributes do you want the replacement to pick up? Blue bold or red italic? – Martin Packer Aug 05 '22 at 07:16

1 Answers1

0

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]):
Vvamp
  • 414
  • 4
  • 12