I have two PPTs (File1.pptx and File2.pptx) in which I have the below 2 lines
XX NOV 2021, Time: xx:xx – xx:xx hrs (90mins)
FY21/22 / FY22/23
I wish to replace like below
a) NOV 2021
as NOV 2022
.
b) FY21/22 / FY22/23
as FY21/22 or FY22/23
.
But the problem is my replacement works in File1.pptx
but it doesn't work in File2.pptx
.
When I printed the run text, I was able to see that they are represented differently in two slides.
def replace_text(replacements:dict,shapes:list):
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
print(cur_text)
print("---")
new_text = cur_text.replace(str(match), str(replacement))
run.text = new_text
In File1.pptx, the cur_text
looks like below (for 1st keyword). So, my replace works (as it contains the keyword that I am looking for)
But in File2.pptx, the cur_text
looks like below (for 1st keyword). So, replace doesn't work (because the cur_text doesn't match with my search term)
The same issue happens for 2nd keyword as well which is FY21/22 / FY22/23
.
The problem is the split keyword could be in previous or next run from current run (with no pattern). So, we should be able to compare a search term with previous run term (along with current term as well). Then a match can be found (like Nov 2021) and be replaced.
This issue happens for only 10% of the search terms (and not for all of my search terms) but scary to live with this issue because if the % increases, we may have to do a lot of manual work. How do we avoid this and code correctly?
How do we get/extract/find/identify the word that we are looking for across multiple runs (when they are indeed present) like CTRL+F and replace it with desired keyword?
Any help please?
UPDATE - Incorrect replacements based on matching
Before replacement
After replacement
My replacement keywords can be found below
replacements = { 'How are you?': "I'm fine!",
'FY21/22':'FY22/23',
'FY_2021':'FY21/22',
'FY20/21':'FY21/22',
'GB2021':'GB2022',
'GB2020':'GB2022',
'SEP-2022':'SEP-2023',
'SEP-2021':'SEP-2022',
'OCT-2021':'OCT-2022',
'OCT-2020':'OCT-2021',
'OCT 2021':'OCT 2022',
'NOV 2021':'NOV 2022',
'FY2122':'FY22/23',
'FY2021':'FY21/22',
'FY1920':'FY20/21',
'FY_2122':'FY22/23',
'FY21/22 / FY22/23':'FY21/22 or FY22/23',
'F21Y22':'FY22/23',
'your FY20 POS FCST':'your FY22/23 POS FCST',
'your FY21/22 POS FCST':'your FY22/23 POS FCST',
'Q2/FY22/23':'Q2-FY22/23',
'JAN-22':'JAN-23',
'solution for FY21/22':'solution for FY22/23',
'achievement in FY20/21':'achievement in FY21/22',
'FY19/20':'FY20/21'}