9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonでクリップボードを操作するモジュール「pyperclip」をLinux(Ubuntu)にインストールした時のメモ。

Posted at

Pythonでクリップボードを操作する**[pyperclip]**というモジュールをインストールしましたが、スッといかなかったのでメモしておきます。

インストール

Python3にインストールしたかったので次のコマンドを実行しました。
pip3 install pyperclip

なにやらおかしい

インポートは問題ないようです。

>>> import pyperclip

クリップボードに”Hello”をコピーてから次のコードで変数[text]にクリップボードの内容を代入します。

## 変数[text]にクリップボードの内容を代入する。
>>> text = pyperclip.paste()

すると、なにやらエラーのようです。

pyperclip.PyperclipException: 
    Pyperclip could not find a copy/paste mechanism for your system.
    For more information, please visit https://pyperclip.readthedocs.io/en/latest/introduction.html#not-implemented-error 

「コピペに必要な仕組みが見つかりません。詳しくはこちらを見てね。」的なことだと思います。(たぶん…)

どうやらLinux版で出るエラーのようで、上記のサイトをGoogle翻訳すると以下のうち1つをインストールする必要があるみたいです。

  • sudo apt-get install xsel
  • sudo apt-get install xclip
  • pip install gtk to install the gtk
  • pip install PyQt4 to install the PyQt4

上の2つはインストールするとコマンドラインからクリップボードを操作したり、中クリックでペーストできるようになったりと便利そうなので両方共インストールしましたが、下の2つはインストールできませんでした。

使い方

## クリップボードに”Hello”を代入する。
>>> pyperclip.copy("Hello")

## 変数[text]にクリップボードの内容を代入する。
>>> text = pyperclip.paste()

## 変数[text]の内容を表示する。
>>> text
'Hello'

WindowsやMacOSやiOSの場合

このモジュールはWindowsやMacOSでも動作しますが同じようなエラーが発生することはないようです。

iOSのPythonista3には対応してないのかOSerrorで動作しませんが、より強力な**[clipboard]**モジュールが標準でインストールされてます。

9
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?