Copy file content to clipboard on Windows

Asked

Viewed 2,951 times

1

Hello, I am making a software for a forum, where members consult the patterns of posts by the software, I am storing these patterns in files and asking Python to read these files and display them, so the user copies the pattern.

Now I want to ask Python to copy the contents of the default file to the Clipboard (which is copy and paste). So not needing the user to select the text and copy, I want the software to do it alone, but how do I do it? I searched a lot and did not find.

I’ll leave a print the output data I want to copy to (copy Windows Paste). Ignore the other markings.

data output http://www.imgfans.com.br/i777/SirGates/erro.png

2 answers

2

For Windows you can use the module win32clipboard, if you prefer an alternative cross-Platform, the module pyperclip.

Example of module usage win32clipboard (untested):

import win32clipboard

win32clipboard.OpenClipboard()

with open('arquivo', 'r') as f:
   conteudo = f.readlines()
   win32clipboard.SetClipboardText(conteudo)
win32clipboard.CloseClipboard()
  • win32clipboard gives error... Tells that the name of this module does not exist.. I will try the other

  • @Leonardovilarinho This module is part of the Pywin32.

  • I decided using Clipboard... This one to copy inside the file needs to have two parameters, the type and the file (I think) then I tried a few times and could not

1


I used that code:

import clipboard

clipboard.paste() 
with open('teste.txt', 'r') as f:
     conteudo = f.read()     
     clipboard.copy(conteudo)

Follow that video to install the Clipboard.

In the video, you then go to DOS and enter C:/Pythonxx/Scripts and give the command pip install clipboard

Browser other questions tagged

You are not signed in. Login or sign up in order to post.