r/OfficeScripts • u/SoberIRL • Mar 06 '13
[Request] Replace all instances of text "xyz" with "abc" in multiple text files across multiple directories
I decided this week to start learning Python, because I'm certain I can do this. I guess I'm looking more for guidance than I am for someone to just give me code. If this task is way beyond what a person can learn to do in, say, 4 weeks - those comments would also be appreciated.
My office has a daily task to download eight text documents from a website. The first tedious, repetitive step in this process is that we must do a bit of text replacement. Notepad has a decent 'find' and 'replace all' setup, but I imagine this can be completely automated.
Once I have a solid grasp of Python, where will I go from there? How would this code be implemented for the other people in my office? Ideally, there would be a .exe or something they'd double-click, and then they'd browse to the parent directory, and hit 'run.' In what framework will I be building that simple GUI?
I assume that, as I get deeper into understanding Python, my understanding of the related tools and so forth will come. Thanks!
View all comments
3
u/OCHawkeye14 Mar 06 '13
This would maybe be better posted in /r/learnpython but I can tell you that some useful code snippets would be the following:
This would open a file named "file.txt", dump the contents to a variable named "contents", and close the file.
Now that we have the contents, we do stuff with those contents. Want to know how many times "xyz" shows up in those contents?
Want to replace that string with another string "abc"? That's not hard, but is a multi-step process. 1) Find where "xyz" starts, 2) find where "xyz" ends, 3) delete start->end, 4) add "abc" to the start location, 5) continue through the document and find the next occurrence of "xyz"
Now just rewrite the file.
Hopefully this helps.