Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace only based on full match and not on partial match #1961

Open
SSMK-wq opened this issue Jul 15, 2022 · 4 comments
Open

Replace only based on full match and not on partial match #1961

SSMK-wq opened this issue Jul 15, 2022 · 4 comments

Comments

@SSMK-wq
Copy link

SSMK-wq commented Jul 15, 2022

Versions of xlwings, Excel and Python (e.g. 0.24,Excel 2019, Python 3.7)

This is related to this issue 1945

I have an excel sheet like below and also attached the file

image

dummy.xlsx

As you can see my input string contains year values.

My replacement pattern is like below

FY1920 should be replaced as FY2021
FY2021 should be replaced as FY2122
FY2122 should be replaced as FY2223
FY19 should be replaced as FY20
FY20 should be replaced as FY21
FY21 should be replaced as FY22

But the problem is Replace method is not looking for exact full match

How can I tell the xlwings to replace only when the text contains only the search string fully (and is not followed by any other string/number).

ex: When text contains only FY20, it should be replaced as FY21. It should not be replaced when part of the text contains FY20 (in cases like FY2021)

Therefore, you can see, my actual output is incorrectly replaced

image

This is the code that I tried

    sheet1.used_range.api.Replace("FY19", "FY20")
    sheet1.used_range.api.Replace("FY20", "FY21")
    sheet1.used_range.api.Replace("FY21", "FY22")
    sheet1.used_range.api.Replace("FY2122", "FY22/23")
    sheet1.used_range.api.Replace("FY2021", "FY2122")
    sheet1.used_range.api.Replace("FY1920", "FY2021")
    sheet1.used_range.api.Replace("FY18/19", "FY1920")

How to do we instruct Xlwings to replace keywords based on condition?

For ex: In a text, it could be sometimes All FY20,FY21. Here the replacement should happen only when my search string is FY20,FY21. else it should not replace for individual keywords like (FY20 or FY21 etc)

@fzumstein
Copy link
Member

Can you try:

from xlwings.constants import LookAt

sheet1.used_range.api.Replace("FY19", "FY20", LookAt.xlWhole)

@SSMK-wq
Copy link
Author

SSMK-wq commented Jul 18, 2022

hmm. No, this doesn't replace "FY19" as "FY20".

@fzumstein
Copy link
Member

Works for me: it solves the following issue:

ex: When text contains only FY20, it should be replaced as FY21. It should not be replaced when part of the text contains FY20 (in cases like FY2021)

import xlwings as xw
from xlwings.constants import LookAt

book = xw.books.active
sheet1 = book.sheets[0]
sheet1.used_range.api.Replace("FY20", "FY21", LookAt.xlWhole)

@SSMK-wq
Copy link
Author

SSMK-wq commented Jul 18, 2022

@fzumstein - Thanks. you are right. but just realized the cell was shrinked and had a text within it. Apologies. I didn't realize that earlier.
something like below. In this case would LookAt help? may be that's why it didn't work for me. Can it take regex etc?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants