1

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?

Leo Gaunt
  • 711
  • 1
  • 8
  • 29

2 Answers2

1

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()
Asocia
  • 5,935
  • 2
  • 21
  • 46
  • I'd need to not to do a new line but actually try and press the enter key because it is for something else I have been programming and newline won't do that unfortunately – Leo Gaunt Jul 23 '20 at 17:07
  • I admit the wording was slightly ambiguous but by press enter I meant a keystroke – Leo Gaunt Jul 24 '20 at 22:18
1

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

Matt
  • 84
  • 9
  • I'd need to not to do a new line but actually try and press the enter key because it is for something else I have been programming and newline won't do that unfortunately – Leo Gaunt Jul 23 '20 at 17:07
  • 1
    @LeoGaunt try using the pyautogui library to simulate key strokes https://stackoverflow.com/questions/52842667/simulate-key-presses-in-age-of-empires-3 – Matt Jul 23 '20 at 17:11
  • 1
    perfect, that was what I needed – Leo Gaunt Jul 23 '20 at 17:16
  • I edited the post to have the solution, please accept the answer – Matt Jul 23 '20 at 17:25
  • Was going to ask you to do that anyway- thanks for the help – Leo Gaunt Jul 23 '20 at 17:53