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

Question - Efficient Find and replace using Xlwings python #1945

Closed
SSMK-wq opened this issue Jun 30, 2022 · 2 comments
Closed

Question - Efficient Find and replace using Xlwings python #1945

SSMK-wq opened this issue Jun 30, 2022 · 2 comments

Comments

@SSMK-wq
Copy link

SSMK-wq commented Jun 30, 2022

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

I have an excel sheet like below

image

I would like to do the below

a) a) Add 1 to all year values. ex: EVENT 2021 should be EVENT 2022

b) Similarly FY_1920 becomes FY_2021 and FY_2021 becomes FY_2122 AND FY_2122 becomes FY2223

c) After step a) and b), save the file.

But the problem is each excel file has 12 sheets and we need to do step a) and b) for all 12 sheets.

Since the file is strictly formatted, I would like to access these cells in excel wherever those strings appear and do the replacements in excel file directly

So, I tried the below xlwings approach

sheet.range("A1:ZZZ1045678").api.Replace("EVENT 2021", "EVENT 2022")
sheet.range("A1:ZZZ1045678").api.Replace("FY_1920", "FY_2021")
sheet.range("A1:ZZZ1045678").api.Replace("FY_2021", "FY_2122")
sheet.range("A1:ZZZ1045678").api.Replace("FY_2122", "FY_2223")
sheet.range("A1:ZZZ1045678").api.Replace("FY1920", "FY2021")
sheet.range("A1:ZZZ1045678").api.Replace("FY2021", "FY2122")
sheet.range("A1:ZZZ1045678").api.Replace("FY2122", "FY2223")

Is there any better way to find and replace strings in excel using xlwings?

I expect my output to be like as below

image

@fzumstein
Copy link
Member

The approach seems reasonable to me, except that you could use used_range:

sheet.used_range.api.Replace(...)

@SSMK-wq
Copy link
Author

SSMK-wq commented Jul 4, 2022

@fzumstein - Cool. your suggestion works much better. but one minor issue. When I replace FY_1920 with FY_2021, it works. But in the subsequent lines, I also want to replace FY_2021 (from last year) with FY_2122. But what it does, it overwrites all the values of FY_2021 with FY_2122 (even from previous code line which did the replacement). So, my output ends up being like below

image

So, what I did was reordered the code lines like below. And this works. But is this the only way?

sheet1.used_range.api.Replace("EVENT 2021", "EVENT 2022")
sheet1.used_range.api.Replace("FY_2122", "FY_2223") # I put the latest year first. So, they don't over write like my earlier code shown above
sheet1.used_range.api.Replace("FY_2021", "FY_2122")
sheet1.used_range.api.Replace("FY_1920", "FY_2021")
sheet1.used_range.api.Replace("FY2122", "FY2223")
sheet1.used_range.api.Replace("FY2021", "FY2122")
sheet1.used_range.api.Replace("FY1920", "FY2021")

How can I let xlwings know that it shouldn't touch/replace the words from location which it already did? Unfortunately, we don't have any pattern with cell number/location. These values could be present anywhere in the file.

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