1

I know you can do this with, pyautogui. But what would you use as an alternative?

  • Possible duplicate of [How do I copy a string to the clipboard on Windows using Python?](https://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python) – wwii Sep 10 '19 at 14:03

2 Answers2

0

You can use pyperclip to help you on that.

>>> import pyperclip
>>> import os
>>> pyperclip.copy(os.path.basename(DIR).split(" ")[0])

You can also paste it with paste()

NOTE: As said in the docs...

Currently only handles plaintext.

On Windows, no additional modules are needed.

On Mac, this module makes use of the pbcopy and pbpaste commands, which should come with the os.

On Linux, this module makes use of the xclip or xsel commands, which should come with the os. Otherwise run “sudo apt-get install xclip” or “sudo apt-get install xsel” (Note: xsel does not always seem to work.)

Otherwise on Linux, you will need the gtk or PyQt4 modules installed.

Alexander Santos
  • 1,458
  • 11
  • 22
0

You can use pyperclip which is a cross platform module handle clipboard:

import os
import pyperclip

filepath = 'C:/mycode/code.py'
filename = os.path.basename(filepath)

# argument in split should enter the sep of two word, there is .
first_word = filename.split('.')[0]


pyperclip.copy(first_word)

If you don't want install a third party module, os.system(f'echo {first_word} | clip') maybe a easy method on Windows instead final statement.

bruce
  • 462
  • 6
  • 9