#100DaysOfCode in Python Transcripts
Chapter: Days 67-69: Copy and Paste with Pyperclip
Lecture: Demo: Pyperclip text replacer
0:00 Okay for this script we're going
0:02 to create a text replacer.
0:04 So this is a quick script that will take in,
0:07 off the clipboard, some text,
0:10 and will allow you to then substitute
0:12 certain words in that text with other words,
0:16 and then paste it back to the clipboard.
0:19 Again, this is something I've used and I keep handy,
0:22 because there's a few times,
0:24 especially at work that I need to do stuff like this.
0:26 So let's import pyperclip.
0:33 Let's throw in this as usual.
0:38 Let's create our functions first.
0:41 We need to create a function
0:42 that will paste in the information, right?
0:45 So paste_from_clipboard.
0:47 I'm making these very detailed names,
0:50 just so we know what we're talking about.
0:52 And we'll say the text
0:55 that we're going to deal with is pyperclip.paste
1:00 And we'll return that.
1:03 So, return text.
1:06 I'm keeping this super clean and simple.
1:09 So we've got our text, we've read it in from the clipboard,
1:12 and we're going to return it.
1:16 Now we need to take that text,
1:19 and replace it.
1:22 We're going to do that with a replace_text.
1:26 So def replace_text.
1:30 Let's read in old text.
1:32 We'll just change the name like that
1:34 to make it interesting and descriptive.
1:38 Now what do we want to do?
1:39 We want to replace some text,
1:42 so we're going to ask the user what they would like to replace,
1:45 rather than hard code it in.
1:47 So we'll just create a target, is input:
1:52 What would you like to replace?
1:58 Then we're going to actually do the:
2:01 What do they want to replace it with?
2:04 First, they're specifying the word
2:06 they would like to have replaced,
2:07 and now they're saying what they want
2:09 to have it replaced with.
2:11 So replacement = input,
2:16 and what would you like to replace it with?
2:25 Let's just hit enter.
2:26 There we go.
2:33 We're going to build a new text block
2:36 after we've done the replacement.
2:38 To do that, we're going to go new text is...
2:41 So old text.replace,
2:45 cause we can do that.
2:47 It's Python, it's cool!
2:49 Old text.replace target,
2:53 and replacement,
2:55 and that just does a simple search for the target,
2:58 and then replaces it with the replacement text.
3:03 Then we'll return that.
3:04 Return new text.
3:08 Let's call these down here,
3:09 so first we're going to say old text,
3:14 cause remember we're calling that in here?
3:16 Old text is...
3:27 and then new text
3:31 is replace text
3:36 and loading in the old text.
3:40 And now we need to copy it back.
3:44 It's going to be similar to the paste_from_clipboard,
3:46 we're going to def copy_to_clipboard.
3:52 And we'll load the new text into that.
3:56 And then all it needs to do is run that same pyperclip.
4:04 New text.
4:06 Now obviously, this is a simple enough script,
4:08 you don't actually have to break it down into functions.
4:12 I just thought that'd be much easier
4:13 for displaying that.
4:16 Then we'll just print a little message saying,
4:19 the new string is now copied to the clipboard.
4:27 That's it.
4:31 So we'll get rid of the bright, white space.
4:33 Let's call that function here, okay?
4:36 And we go copy to clipboard
4:40 New text.
4:44 So we'll save that.
4:47 Let me just copy a block of text for us to actually do this.
4:52 We will do this.
4:57 So here's the block of text, alright?
5:03 Let's run the script.
5:07 Alright, what would you like to replace?
5:08 Let's replace Julien.
5:12 And what would you like to replace it with?
5:16 The new string is copied to the clipboard.
5:19 What's cooler than cool?
5:21 Now that's a blatant lie,
5:22 but that's a great demonstration point.
5:26 Let's try it again.
5:27 So we'll go run the script.
5:31 What would you like to replace?
5:32 Let's replace Bob with Mike.
5:37 New string is copied, let's hit paste.
5:39 What's cooler than cool?
5:44 I'm not going to comment.
5:46 Sorry, Mike.
5:49 Let's run it one more time.
5:52 What would you like to replace?
5:54 Let's replace Mike with ice cold.
6:00 And paste. What's cooler than cool?
6:02 Ice cold.
6:03 And that's it.
6:04 Really cool, simple script.
6:05 Something usable, I'm sure it's something
6:07 you can make use of in day-to-day life.
6:10 Enjoy it, use it, and that's pretty's much...