Leo Gaunt
Leo Gaunt

Reputation: 693

How do I make python copy and paste into a certain application using a keystroke?

I am trying to make a piece of code that makes something like this

words = "hello my name is Leo"

Into this

hello my name is Leo printed on separate lines on apple notes

This is printed on apple notes but what I want it to do is to copy a the word of the text and paste it, then press the enter key and type another word.

Currently I have this

import pyperclip
words = "hello my name is Leo"

split = words.split()

for x in range(0,len(split)):
    pyperclip.copy(split[x])

I am not sure how to make it press enter (with a keystroke) and be able to use automate it to do it in another application. Can anyone help?

Upvotes: 1

Views: 727

Answers (2)

Matt
Matt

Reputation: 84

It sounds to me like you're looking for the newline character. To "press enter" use \n that will tell it to go to a new line.

You could really do something like

words = "hello\nmy\nname\nis\n"

Using the newline character would also eliminate the need for the for loop you used.

EDIT:

To simulate the keystrokes, use pyautogui library, that will work. See: Simulate key presses in Age of Empires 3

Upvotes: 1

sahinakkaya
sahinakkaya

Reputation: 6056

Split your text by space then join it with "\n" characters. Then you can copy and paste it with pyperclip:

import pyperclip
words = "hello my name is Leo"
edited_text = "\n".join(words.split())
pyperclip.copy(edited_text)
...
pyperclip.paste()

Upvotes: 1

Related Questions