#100DaysOfCode in Python Transcripts
Chapter: Days 67-69: Copy and Paste with Pyperclip
Lecture: Demo: Pyperclip text replacer

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

Talk Python's Mastodon Michael Kennedy's Mastodon