Python3 + pyperclip that rewrites the copied text

Planning

I wanted to write a program that was extremely wasteful, rewriting the copied text on a regular basis, so I'll try it. The source code is so short that it's probably for beginners.

Think about the procedure

  1. Get the copied text
  2. Rewrite
  3. Paste to clipboard
  4. Repeat regularly

However, only a part of the content will be rewritten.

environment

let's try it!

Try using pyperclip

First, get the text copied to the clipboard. You can use pyperclip for this.

command prompt


pip install pyperclip 

You should be able to install it by typing in the command prompt. Let's copy the previous sentence and run the program.

sample01.py


import pyperclip, re

copy_text = str(pyperclip.paste())
print(copy_text)

Execution result


However, it is too obvious and not interesting to rewrite everything.
So why not change only part of it?

I succeeded in getting the copied text, so I try copying the text to the clipboard.

sample02.py


copy_text = 'hello world!'

print(copy_text)
pyperclip.copy('\n'.join(copy_text))

Execution result


My name is Alice
hello world!

rewrite

Let's rewrite the copied text.

It's not interesting to change everything (you don't even need to get the text in the first place), so I want to change only a part. For example, numbers. When the numbers come out, replace them with random numbers.

I think that the sentences to be copied are often in Japanese, so let's change the alphabet. This will be rewritten only when a specific character string appears.

sample03.py


import pyperclip, re

copy_text = str(pyperclip.paste())
print(copy_text)

new = re.sub(r'[\d]', '*', copy_text)
new = re.sub(r'day|best|hello', '****', new)

if len(copy_text) > 0:
    pyperclip.copy('\n'.join(new))
    print('Rewrite:', new)
else:
    print('Nothing')

Replace strings with re.sub. r''is a regular expression. I was thinking about replacing it with a random number or changing it to a random string, but it didn't work. I'm sure someone will think good about it.

Execution result


I will do my best day to day!
Rewrite: I will do my **** **** to ****!

By the way, the English text I copied is a Google translation of "I'll do my best today!" I'm looking forward to the second term of New Game.

Start regularly

The last oshigoto. By executing Thread from Thread, it will be executed every 5 seconds. The program up to the above was partially changed.

copyChange.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

u"""Rewrite the text on the Hoge clipboard without permission
Example sentence: I will do my best day to day 2017!
"""

import pyperclip, re, threading, random


def task():
    copy_text = str(pyperclip.paste())

    new = re.sub(r'[\d]', str(random.randint(0, 9)), copy_text)
    new = re.sub(r'day|best|hello', '****', new)

    if len(copy_text) > 0:
        pyperclip.copy(''.join(new))
        print('---Rewrite Complete!---')
    else:
        print('---Failure---')
    th = threading.Timer(5, task)
    th.start()

t = threading.Thread(target=task)
t.start()

I rewrote it to change the number using random so that I can see that it is repeating regularly. At the same time, 2017 has been added to the example sentences. Also, I deleted it because it would be an obstacle to print the copied text when executing it.

Execution result


---Rewrite Complete!---
---Rewrite Complete!---
---Rewrite Complete!---

Clipboard


I will do my **** **** to **** 4444!
I will do my **** **** to **** 7777!
I will do my **** **** to **** 8888!

Thank you for your hard work.

bonus

The clipboard cannot be rewritten unless this program is started. Therefore, I will present a method to start it automatically in order to further increase the degree of violence.

This time it is assumed to be Windows, so you can use the task scheduler. I think that other OSs have their own automatic startup methods, so if you want to be bullied by your own PC, you should definitely search and try it. Start Task Scheduler

You may be able to do it with schtasks at the command prompt. I've never used it, but I don't know.

Reference material

-Regular expression operation

apology

--I received an edit request that the code is not displayed correctly. It has been fixed now. Even if it is displayed on kobito, it may not work well on Qiita. Thank you for your report and correction.

Recommended Posts

Python3 + pyperclip that rewrites the copied text
[python] Move files that meet the conditions
[Python] A program that rounds the score
Extract lines that match the conditions from a text file with python
My pyperclip (python)
The one that displays the progress bar in Python
[Python] A program that counts the number of valleys
[Python] Evaluate the facial expressions that appear on the face
[Python] Generate random numbers that follow the Rayleigh distribution
Find the part that is 575 from Wikipedia in Python
Python program that looks for the same file name
PHP and Python samples that hit the ChatWork API
A memo that I touched the Datastore with python
I felt that I ported the Python code to C ++ 98.
Modules that may go through the shell in Python
The attitude that programmers should have (The Zen of Python)
[Python] A program that compares the positions of kangaroos.
A Python program that converts ical data into text
Python OpenCV tried to display the image in text.
Clustering text in Python
Find the maximum Python
python text aloud (pyttsx3)
the zen of Python
Text processing in Python
[Python] Split the date
Creating a Python script that supports the e-Stat API (ver.2)
The story that `while queue` did not work in python
Consideration for Python decorators of the type that passes variables
One liner that randomly rewrites the startup time of cron
The story that Python stopped working with VS Code (Windows 10)
Miscellaneous notes that I tried using python for the matter
[Python] A program that finds the most common bird types
Treat the Interface class like that with Python type annotations
A Python script that compares the contents of two directories