__init__.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. Pyperclip
  3. A cross-platform clipboard module for Python. (only handles plain text for now)
  4. By Al Sweigart al@inventwithpython.com
  5. BSD License
  6. Usage:
  7. import pyperclip
  8. pyperclip.copy('The text to be copied to the clipboard.')
  9. spam = pyperclip.paste()
  10. if not pyperclip.copy:
  11. print("Copy functionality unavailable!")
  12. On Windows, no additional modules are needed.
  13. On Mac, the module uses pbcopy and pbpaste, which should come with the os.
  14. On Linux, install xclip or xsel via package manager. For example, in Debian:
  15. sudo apt-get install xclip
  16. Otherwise on Linux, you will need the gtk, qtpy or PyQt modules installed.
  17. qtpy also requires a python-qt-bindings module: PyQt4, PyQt5, PySide, PySide2
  18. gtk and PyQt4 modules are not available for Python 3,
  19. and this module does not work with PyGObject yet.
  20. """
  21. __version__ = '1.5.27'
  22. import platform
  23. import os
  24. import subprocess
  25. from .clipboards import (init_osx_clipboard,
  26. init_gtk_clipboard, init_qt_clipboard,
  27. init_xclip_clipboard, init_xsel_clipboard,
  28. init_klipper_clipboard, init_no_clipboard)
  29. from .windows import init_windows_clipboard
  30. # `import qtpy` sys.exit()s if DISPLAY is not in the environment.
  31. # Thus, we need to detect the presence of $DISPLAY manually
  32. # and not load qtpy if it is absent.
  33. HAS_DISPLAY = os.getenv("DISPLAY", False)
  34. CHECK_CMD = "where" if platform.system() == "Windows" else "which"
  35. def _executable_exists(name):
  36. return subprocess.call([CHECK_CMD, name],
  37. stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
  38. def determine_clipboard():
  39. # Determine the OS/platform and set
  40. # the copy() and paste() functions accordingly.
  41. if 'cygwin' in platform.system().lower():
  42. # FIXME: pyperclip currently does not support Cygwin,
  43. # see https://github.com/asweigart/pyperclip/issues/55
  44. pass
  45. elif os.name == 'nt' or platform.system() == 'Windows':
  46. return init_windows_clipboard()
  47. if os.name == 'mac' or platform.system() == 'Darwin':
  48. return init_osx_clipboard()
  49. if HAS_DISPLAY:
  50. # Determine which command/module is installed, if any.
  51. try:
  52. # Check if gtk is installed
  53. import gtk # noqa
  54. except ImportError:
  55. pass
  56. else:
  57. return init_gtk_clipboard()
  58. try:
  59. # qtpy is a small abstraction layer that lets you write
  60. # applications using a single api call to either PyQt or PySide
  61. # https://pypi.org/project/QtPy
  62. import qtpy # noqa
  63. except ImportError:
  64. # If qtpy isn't installed, fall back on importing PyQt5, or PyQt5
  65. try:
  66. import PyQt5 # noqa
  67. except ImportError:
  68. try:
  69. import PyQt4 # noqa
  70. except ImportError:
  71. pass # fail fast for all non-ImportError exceptions.
  72. else:
  73. return init_qt_clipboard()
  74. else:
  75. return init_qt_clipboard()
  76. pass
  77. else:
  78. return init_qt_clipboard()
  79. if _executable_exists("xclip"):
  80. return init_xclip_clipboard()
  81. if _executable_exists("xsel"):
  82. return init_xsel_clipboard()
  83. if _executable_exists("klipper") and _executable_exists("qdbus"):
  84. return init_klipper_clipboard()
  85. return init_no_clipboard()
  86. def set_clipboard(clipboard):
  87. global copy, paste
  88. clipboard_types = {'osx': init_osx_clipboard,
  89. 'gtk': init_gtk_clipboard,
  90. 'qt': init_qt_clipboard,
  91. 'xclip': init_xclip_clipboard,
  92. 'xsel': init_xsel_clipboard,
  93. 'klipper': init_klipper_clipboard,
  94. 'windows': init_windows_clipboard,
  95. 'no': init_no_clipboard}
  96. copy, paste = clipboard_types[clipboard]()
  97. copy, paste = determine_clipboard()
  98. __all__ = ["copy", "paste"]
  99. # pandas aliases
  100. clipboard_get = paste
  101. clipboard_set = copy