2

How do you iterate through a table and find and replace text? I'm able to go through text boxes as below but need to be able to access and edit information in a table:

for slide in prs.slides:
    for shape in slide.shapes:
        if shape.has_text_frame: 
            j = 0
            k = 0
            for i in searchList:  
                #find the search term from the searchList
                searchString = searchList[j]
                j+=1
                #replace the search term from the dataframe
                dfIndexName = dfIndex.keys()[k]
                k+=1
                replaceString = df.at[0, dfIndexName] 
                if (shape.text.find(searchString))!=1: 
                    text_frame = shape.text_frame
                    cur_text = text_frame.paragraphs[0].runs[0].text
                    new_text = cur_text.replace(searchString, str(replaceString))
                    text_frame.paragraphs[0].runs[0].text = new_text  

Thanks!

simplesam
  • 61
  • 7

1 Answers1

2

I'm not sure what you want to do with searchList, but this is how you can access cells in a table.

  for slide in prs.slides:
    for shape in slide.shapes:
      if shape.has_table: 
        table = shape.table
        for cell in table.iter_cells():
          # here you can access the text in cell by using
          # cell.text
          # just remember that the shape object refers to the table in this context not the cell
Saleh
  • 173
  • 2
  • 12
  • SearchList = Find words in the SearchList and replace them with words in the df I'm getting "iter_cells is not defined", it works if I use "table.iter_cells()" but then I get caught with "'GraphicFrame' obect has no attribute 'text'" because of the "if (shape.text.find(searchString))!=1:" line – simplesam May 13 '20 at 02:33
  • so what is in `searchList` exactly? Because you're iterating over searchList in the for loop but then you never use `i` and instead try to access it by index `j`. If you use `i` in the for loop that would be the same as `searchString`. Also you're getting that error because you are no longer trying to access `text` on shape, but you should access it on the `cell` object. I've updated my answer. – Saleh May 13 '20 at 15:23
  • searchList is a list of words Trying to find the words in searchList and replace the words in searchString This is how I'm thinking about it, let me know the errors: Loop through all the slide Loop through all the shapes in the slides If the shape is a table, then iterate through the cells Iterate through the words in SearchList (if I use 'for j in ...' then I get a str error) to find matches Replace the words in SearchList with words in dfIndex So where should I refer to the cell.text? I've tried a couple of things but it keep throwing errors – simplesam May 18 '20 at 08:58
  • I'm not following honestly, can you explain your use case and give a small example of what you are trying achieve. – Saleh May 19 '20 at 03:15
  • Thanks for your patience: I have an excel sheet and it has rows of information in columns. It reads the list and stores that information in a dataframe. I want to take that information and put it in a powerpoint presentation. There's a table in the powerpoint template presentation with the named variables in it. I just want to find and replace in the named variables in the template with the information in the dataframe. So ideally, I'm taking information from Excel and doing a find and replace in ppt – simplesam May 26 '20 at 22:17