4

I am trying to print the slide number and store it in a list in python, from a power point presentation using some specific String. Suppose I have a string named "CR". If CR is present in a slide then it will print that slide number and also store it in a list. My code is given below, but unfortunately it prints all the slide index numbers.

from pptx import Presentation
word = 'CR'

ppt = Presentation('D:\\Users\\Shaon_Paul\\pptss\\abc.pptx')
for slide in ppt.slides:

   slides = ppt.slides
   if (word.find('CR') != -1):
       print(ppt.slides.index(slide) + 1 )
   else: 
       print ("Doesn't contains given string") 

Using the code above it prints all the slide number in the ppt but I want to print only the slide numbers which have the string "CR" and store them in a list. Need help to resolve this issue.

beliz
  • 402
  • 1
  • 5
  • 25
Shaon
  • 133
  • 1
  • 13

1 Answers1

0

the problem is that in your if condition you're testing whether your variable word contains "CR", which it does because it's actually equal to "CR", that's why the return value is always True. I think what you should do is replace it with something like slide.find('CR') (assuming that slide is a string).

briwa
  • 46
  • 6
  • :got error AttributeError: 'Slide' object has no attribute 'find' – Shaon Mar 14 '19 at 13:12
  • So that means `slide` is not a string so you can't call the function find for it. What you should do is extract the text from the slide first, and then search for the word. This might be useful https://python-pptx.readthedocs.io/en/latest/user/text.html#accessing-the-text-frame – briwa Mar 14 '19 at 13:16
  • :My final Aim is to identify the slide number and store that number in a list.I am able to extract all the text but how to find the slide number after that? – Shaon Mar 14 '19 at 13:25
  • I have tested my code above with different keywords...If the keyword is different and its not in the slide the it print "Doesn't contains given string".That means above cod is right.Is there any other view that you can suggest? – Shaon Mar 14 '19 at 13:38