Automate the boring Stuff with Python learning Note 1

Source: Internet
Author: User
Tags gtk

The automate the boring Stuff with Python was finished, and began to do the first project according to the gourd painting scoop.

#! python3# pw.py - an insecure password locker program. password = {' email ':  ' f7minlbdduvmjuxesskhfhtxftjvb6 ',              ' blog ':  ' VMALVQYKAXIVH5G8V01IF1MLZF3SDT ',              ' luggage ':  ' 12345 '}             import sys, pyperclipif len (SYS.ARGV)  < 2:     print (' Usage: python pw.py [account] - copy account  password ')     sys.exit ()     account = sys.argv[1]         # first command line arg is the  account namerif account in PASSWORD:    pyperclip.copy  ( Password[account])     print (' Password for  '  + account +  '  coopied to clipboard. ') Else:    print (' there is no account named  '  + account)


The above is code, but the runtime prompts no module named Pyperclip, so the internet to find information said to install the module, try: (I found the original PIP install can also be used in Windows, as long as the python can be installed)


C:\users\simmy>pip Install Pyperclip
Collecting Pyperclip
Downloading Pyperclip-1.5.27.zip
Installing collected Packages:pyperclip
Running setup.py Install for Pyperclip
Successfully installed pyperclip-1.5.27
You are using the PIP version 7.1.2, however version 8.1.1 is available.
You should consider upgrading via the ' python-m pip install--upgrade pip ' comm
and.

C:\users\simmy>pip Freeze
pyperclip==1.5.27
You are using the PIP version 7.1.2, however version 8.1.1 is available.
You should consider upgrading via the ' python-m pip install--upgrade pip ' comm
and.

After the end of the error is still prompted, so on the Python idle test:

>>> Import Pyperclip
Traceback (most recent):
File "<pyshell#0>", line 1, in <module>
Import Pyperclip

Importerror:no module named ' Pyperclip '


The same error, later query, found that the author is his own writing module, mentioned here: Https://inventwithpython.com/hacking/chapter2.html:Downloading pyperclip.py
Almost every program in the book uses a custom module I wrote called pyperclip.py. This module provides functions for letting your program copy and paste text to the Clipboard. This module does isn't come with Python, but can download it from:http://invpy.com/pyperclip.py

This file must is in the same folder as the Python program files, you type. (a folder is also called a directory.) Otherwise you'll see this error message when you try to run your program:

Importerror:no module named Pyperclip



Download from this: https://inventwithpython.com/pyperclip.py

Then put: C:\Users\xxx\AppData\Local\Programs\Python\Python35

Problem solving.


pyperclip.py code is as follows:

--------------------------------------------------------------------------------------

"" "pyperclipa cross-platform clipboard module for python.  (only handles  plain text for now) by al sweigart [email protected]bsd  Licenseusage:  import pyperclip  pyperclip.copy (' the text to be  Copied to the clipboard. ')   spam = pyperclip.paste () 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 "   otherwise on  Linux, you will need the gtk or pyqt4 modules installed. the gtk module is not available for python 3, and this  Module does not work with pygobject yet. "" " __version__ =  ' 1.5.6 ' import platform, osfrom subprocess import call,  Popen, pipedef _pastewindows ():    cf_unicodetext = 13     d = ctypes.windll    d.user32.openclipboard (None)      handle = d.user32.getclipboarddata (Cf_unicodetext)     data =  Ctypes.c_wchar_p (handle). Value    d.user32.closeclipboard ()     return  datadef _copywindows (text):    gmem_ddeshare = 0x2000     cf_unicodetext = 13    d&nbSp;= ctypes.windll # cdll expects 4 more bytes in user32. OpenClipboard (None)     try:  # Python 2         if not isinstance (Text, unicode):             text = text.decode (' MBCS ')     except  Nameerror:        if not isinstance (TEXT,&NBSP;STR):             text = text.decode (' MBCS ')      d.user32.openclipboard (None)     d.user32.emptyclipboard ()      hcd = d.kernel32.globalalloc (Gmem_ddeshare, len (Text.encode (' Utf-16-le '))  + &NBSP;2)     pchdata = d.kernel32.globallock (hCd)      ctypes.cdll.msvcrt.wcscpy (Ctypes.c_wchar_p (pchdATA),  text)     d.kernel32.globalunlock (hCd)      D.user32.setclipboarddata (CF_UNICODETEXT,&NBSP;HCD)     d.user32.closeclipboard () def _ Pastecygwin ():    cf_unicodetext = 13    d =  Ctypes.cdll    d.user32.openclipboard (None)     handle =  D.user32.getclipboarddata (Cf_unicodetext)     data = ctypes.c_wchar_p (handle). Value    d.user32.closeclipboard ()     return datadef _ Copycygwin (text):     gmem_ddeshare = 0x2000    cf_ unicodetext = 13    d = ctypes.cdll    try:   # python 2        if not isinstance (Text,  unicode): &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;    text = text.decode (' MBCS ')     except nameerror:         if not isinstance (TEXT,&NBSP;STR):             text = text.decode (' MBCS ')      d.user32.openclipboard (None)     d.user32.emptyclipboard ()     hCd  = d.kernel32.globalalloc (Gmem_ddeshare, len (Text.encode (' Utf-16-le '))  + 2)      pchdata = d.kernel32.globallock (hCd)      ctypes.cdll.msvcrt.wcscpy (Ctypes.c_wchar_p (pchdata),  text)     d.kernel32.globalunlock (hCd)     d.user32.setclipboarddata (CF_UNICODETEXT,&NBSP;HCD)      D.user32.closeclipboard () def _copyosx (text):     text = str (text)      p = popen ([' Pbcopy ', &NBSP; ' W '], stdin=pipe)     try:        # works  on Python 3  (bytes ()  requires an encoding)          p.communicate (Input=bytes (text,  ' utf-8 '))     except typeerror:         # works on Python 2  (bytes ()   only takes one argument)         p.communicate (input= Bytes (text)) Def _pasteosx ():     p = popen ([' Pbpaste ',  ' R '],  Stdout=pipe)     stdout, stderr = p.communicate ()      Return bytes.decode (stdout) DEF&NBSP;_PASTEGTK (): &NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;GTK. Clipboard (). Wait_for_text () def _copygtk (text):    global cb     text = str (text)  &nbsP;  cb = gtk. Clipboard ()     cb.set_text (text)     cb.store () DEF&NBSP;_PASTEQT ():     return str (Cb.text ()) def _copyqt (text):     text =  str (text)     cb.settext (text) def _copyxclip (text):     p  = popen ([' Xclip ',  '-selection ',  ' C '], stdin=pipe)     try:         # works on Python 3  (bytes ()  requires  an encoding)         p.communicate (input=bytes (text,  ' Utf-8 '))     except TypeError:        #  works on python 2  (bytes ()  only takes one argument)          p.communicate (input=bytes (text)) Def _pastexclip ():      p = popen ([' Xclip ',  '-selection ',  ' C ',  '-o '], stdout=pipe)      stdout, stderr = p.communicate ()     return bytes.decode ( STDOUT) Def _copyxsel (text):     p = popen ([' Xsel ',  '-i '], stdin= PIPE)     try:        # works on  python 3  (bytes ()  requires an encoding)          p.communicate (Input=bytes (text,  ' utf-8 '))     except TypeError:         # works on Python 2  (bytes ()  only takes  one argument)         p.communicate (input=bytes (text)) def  _pastexsel ():     p = popen ([' Xsel ',  '-o '], stdout=pipe)      stdout,  Stderr = p.communicate ()     return bytes.decode (stdout) # Determine  the os/platform and set the copy ()  and paste ()  functions  accordingly.if  ' Cygwin '  in platform.system (). Lower ():     _functions =   ' Cygwin '  # for debugging    import ctypes     paste = _pastecygwin    copy = _copycygwinelif os.name  ==  ' NT '  or platform.system ()  ==  ' Windows ':     _functions  =  ' Windows '  # for debugging    import ctypes     paste = _pasteWindows    copy = _copyWindowselif  os.name ==  ' Mac '  or platform.system ()  ==  ' Darwin ':     _ functions =  ' OS&NBSP;X&NBSP;PBCOpy/pbpaste '  # for debugging    paste = _pasteOSX     copy = _copyOSXelif os.name ==  ' POSIX '  or platform.system ()  ==  ' Linux ':    # determine which command/module is  Installed, if any.    xclipexists = call ([' which ',  ' Xclip '),                 stdout=pipe,  stderr=pipe)  == 0    xselexists = call ([' which ',  ' Xsel '],             stdout=pipe, stderr=pipe)  ==  0    gtkInstalled = False    try:         # Check it gtk is installed.         import Gtk        gtkinstalled = true    except  importerror:        pass    if not  gtkinstalled:        # check if pyqt4 is  installed.        PyQt4Installed = False         try:             import pyqt4.qtcore            import  pyqt4.qtgui            pyqt4installed =  True        except ImportError:             pass    # Set one of  The copy & paste functions.    if xclipexists:         _functions =  ' Xclip command '  # for debugging         paste = _pastexclip        copy =  _copyXclip    elif gtkInstalled:         _functions =  ' Gtk module '  # for debugging         paste = _pasteGtk        copy =  _copygtk    elif pyqt4installed:        _ functions =  ' Pyqt4 module '  # for debugging         app = pyqt4.qtgui.qapplication ([]) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CB  = pyqt4.qtgui.qapplication. Clipboard ()         paste = _pasteQt         copy = _copyQt    elif xselExists:   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;#&NBSP;TODO:&NBSP;XSEL&NBSP;DOESN ' T seem to work on  Raspberry Pi  (my test linux environment) . putting this as  the last method tried.        _functions =  ' Xsel command '  # for debugging        paste =  _pasteXsel        copy = _copyXsel     else:        raise exception (' Pyperclip requires  The xclip or xsel application, or the gtk or pyqt4 module. ') else:    raise runtimeerror (' Pyperclip does not support your system. ') 

Automate the boring Stuff with Python learning Note 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.