r/OfficeScripts • • 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!

11 Upvotes

15 comments sorted by

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:

with open('file.txt') as f:
    contents = f.read()

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?

count = contents.count('xyz')

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"

start = 0
for i in range(count):
    start = contents.find('xyz', start)
    end = start+len('xyz')
    contents = contents[:start]+'abc'+contents[end:]
    start = end+1

Now just rewrite the file.

with open('file.txt', 'wb') as f:
    f.write(contents)

Hopefully this helps.

2

u/SoberIRL Mar 06 '13

I figured this wasn't the optimal subreddit, but I know it's a small, new community, and wanted to help will out your front page, heh.

Thanks for this comment. I've only been learning for a few days, but all of the syntax and formatting of your post makes sense as I read it, even though I'm not familiar with a lot of the functions therein. That's exciting!

2

u/throwOHOHaway Mar 06 '13

And we appreciate the support :) I actually did develop a command line tool using /u/OCHawkeye14's code snippet this morning, I'll upload it shortly. It's a command line tool as of right now, and you can get it working once Python 2.7 is installed on your system.

We can work on giving it a frontend over time, but for now, the script I worked on this morning should suit your purposes.