Search code examples
pythonpython-3.xpyperclip

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?


Solution

  • 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