ã¯ããã«ïŒpyperclipãšã¯ïŒ ð€
Pythonã§ããã°ã©ãã³ã°ãããŠãããšããã¹ã¯ãªããã§çæããããã¹ããä»ã®ã¢ããªã±ãŒã·ã§ã³ã«è²Œãä»ãããããããã¯ãä»ã®ã¢ããªã±ãŒã·ã§ã³ããã³ããŒããããã¹ããã¹ã¯ãªããã§åŠçãããããšãã£ãå Žé¢ã«ééããããšããããŸããéåžžãããããæäœã¯æåã§è¡ããŸãããå®åçãªäœæ¥ã§ããã°èªååããããšèããã®ã¯èªç¶ãªããšã§ãã
ããã§æŽ»èºããã®ããä»åã玹ä»ããPythonã©ã€ãã©ãªpyperclip
ã§ããpyperclip
ã¯ãOSã®ã¯ãªããããŒãæ©èœãPythonã¹ã¯ãªããããç°¡åã«ããã€ã¯ãã¹ãã©ãããã©ãŒã ã§å©çšã§ããããã«èšèšããã䟿å©ãªã©ã€ãã©ãªã§ããWindows, macOS, Linuxãšãã£ãäž»èŠãªOSã«å¯Ÿå¿ããŠãããã·ã³ãã«ãªé¢æ°åŒã³åºãã ãã§ããã¹ãã®ã³ããŒïŒããŒã¹ããå®çŸã§ããŸãã
ãã®ããã°èšäºã§ã¯ãpyperclip
ã®åºæ¬çãªäœ¿ãæ¹ãããOSããšã®æ³šæç¹ãå¿çšäŸããšã©ãŒãã³ããªã³ã°ãããã«ã¯WSLç°å¢ã§ã®å©çšãªã©ãpyperclip
ã䜿ãããªãããã®æ
å ±ã網çŸ
çã«è§£èª¬ããŠãããŸããæ¥ã
ã®äœæ¥å¹çåããã¡ãã£ãšãã䟿å©ããŒã«ã®äœæã«åœ¹ç«ã€ããšééããªãã§ãïŒâš
ã€ã³ã¹ããŒã«ïŒããã«å§ãããïŒð
pyperclip
ãå©çšããã«ã¯ããŸãPythonç°å¢ã«ã€ã³ã¹ããŒã«ããå¿
èŠããããŸããPythonã®ããã±ãŒãžç®¡çããŒã«ã§ããpip
ïŒãŸãã¯pip3
ïŒã䜿ãã°ãã³ãã³ãäžã€ã§ç°¡åã«ã€ã³ã¹ããŒã«ã§ããŸãã
ã¿ãŒããã«ïŒã³ãã³ãããã³ããïŒãéãã以äžã®ã³ãã³ããå®è¡ããŠãã ããã
pip install pyperclip
ãããã¯ãPython 3ç°å¢ãæ瀺çã«æå®ããå Žåã¯ã
pip3 install pyperclip
ãå®è¡ããŸãã
Linuxç°å¢ã§ã¯ã
pyperclip
ãã¯ãªããããŒãã«ã¢ã¯ã»ã¹ããããã«è¿œå ã®ããŒã«ãå¿
èŠã«ãªãå ŽåããããŸããå
·äœçã«ã¯ãxclip
ãŸãã¯xsel
ãšããã³ãã³ãã©ã€ã³ãŠãŒãã£ãªãã£ã®ãããããã€ã³ã¹ããŒã«ãããŠããå¿
èŠããããŸããDebian/Ubuntuç³»ã®ãã£ã¹ããªãã¥ãŒã·ã§ã³ã§ã¯ã以äžã®ã³ãã³ãã§ã€ã³ã¹ããŒã«ã§ããŸãã
sudo apt-get update
sudo apt-get install xclip
ãŸãã¯
sudo apt-get update
sudo apt-get install xsel
ãããã®ããŒã«ããªãå Žåãpyperclip
ã¯gtk
ãPyQt5
(ãŸãã¯PyQt4
) ãšãã£ãGUIããŒã«ãããã®Pythonã¢ãžã¥ãŒã«ãæ¢ããŸããããµãŒããŒç°å¢ãªã©GUIããªãå Žåã¯xclip
ãxsel
ã®ã€ã³ã¹ããŒã«ãæšå¥šãããŸãã
詳现ã¯åŸè¿°ã®ããã©ãããã©ãŒã ããšã®éããšæ³šæç¹ãã§è§ŠããŸãã
ã€ã³ã¹ããŒã«ãå®äºããããPythonã¹ã¯ãªãããã€ã³ã¿ã©ã¯ãã£ãã·ã§ã«ã§import pyperclip
ãšèšè¿°ããããšã§ãpyperclip
ã®æ©èœãå©çšã§ããããã«ãªããŸãã
import pyperclip
# ãã㧠pyperclip ã䜿ããç¶æ
ã«ãªããŸããïŒ
åºæ¬çãªäœ¿ãæ¹ïŒã³ããŒïŒããŒã¹ãããã¹ã¿ãŒð
pyperclip
ã®åºæ¬çãªæ©èœã¯éåžžã«ã·ã³ãã«ã§ãäž»ã«2ã€ã®é¢æ°ãèŠããã ãã§äœ¿ãå§ããããšãã§ããŸãã
pyperclip.copy(text)
: æå®ããããã¹ããã¯ãªããããŒãã«ã³ããŒããŸããpyperclip.paste()
: ã¯ãªããããŒãã«ããããã¹ããååŸïŒããŒã¹ãïŒããŸãã
ããã¹ããã¯ãªããããŒãã«ã³ããŒãã (copy())
ã¹ã¯ãªããå
ã§çæããæååããå€æ°ã«æ ŒçŽãããŠããæååãã¯ãªããããŒãã«éãããå Žåãcopy()
é¢æ°ã䜿çšããŸããåŒæ°ã«ã¯ã³ããŒãããæååãæž¡ããŸãã
import pyperclip
text_to_copy = "ããã¯Pythonããã³ããŒãããããã¹ãã§ããð"
pyperclip.copy(text_to_copy)
print("ããã¹ããã¯ãªããããŒãã«ã³ããŒãããŸããïŒ")
ãã®ã³ãŒããå®è¡ãããšã"ããã¯Pythonããã³ããŒãããããã¹ãã§ããð"
ãšããæååãã¯ãªããããŒãã«ã³ããŒãããä»ã®ã¢ããªã±ãŒã·ã§ã³ã§ã貌ãä»ããæäœãè¡ããšãã®ããã¹ããããŒã¹ããããŸãã
ã¯ãªããããŒãããããã¹ããååŸãã (paste())
éã«ãã¯ãªããããŒãã«ã³ããŒãããŠããããã¹ããPythonã¹ã¯ãªããå
ã§å©çšãããå Žåã¯ãpaste()
é¢æ°ã䜿çšããŸãããã®é¢æ°ã¯ãã¯ãªããããŒãã®å
容ãæååãšããŠè¿ããŸãã
import pyperclip
# äºåã«äœãããã¹ããã¯ãªããããŒãã«ã³ããŒããŠãããŠãã ãã
# (äŸ: ãã®æç« ã®äžéšãéžæã㊠Ctrl+C ãŸã㯠Cmd+C)
clipboard_content = pyperclip.paste()
print("ã¯ãªããããŒãã®å
容ïŒ")
print(clipboard_content)
ãã®ã³ãŒããå®è¡ãããšãã¯ãªããããŒãã«çŸåšæ ŒçŽãããŠããããã¹ããååŸãããã³ã³ãœãŒã«ã«åºåãããŸããããã¯ãªããããŒãã«ããã¹ã以å€ã®ããŒã¿ïŒç»åãªã©ïŒãã³ããŒãããŠããå Žåãpaste()
é¢æ°ã¯ç©ºæååïŒ''
ïŒãè¿ãããšããããŸããpyperclip
ã¯åºæ¬çã«ãã¬ãŒã³ããã¹ãã®ã¿ãæ±ãããšã«æ³šæããŠãã ããã
çµã¿åãããäŸïŒã³ããŒããŠããã«ããŒã¹ã
import pyperclip
# 'Hello, Pyperclip!' ãã³ããŒ
pyperclip.copy('Hello, Pyperclip! ð')
print("ã³ããŒå®äºïŒ")
# ããã«ããŒã¹ãããŠå
容ã確èª
pasted_text = pyperclip.paste()
print(f"ããŒã¹ããããå
容: {pasted_text}")
ãã®ããã«ãcopy()
ãšpaste()
ã䜿ãããšã§ãPythonã¹ã¯ãªãããšã¯ãªããããŒãéã§ã®ããã¹ãã®ããåããéåžžã«ç°¡åã«è¡ããŸãã
ãã©ãããã©ãŒã ããšã®éããšæ³šæç¹ ð»ðð§
pyperclip
ã®å€§ããªé
åã®äžã€ã¯ã¯ãã¹ãã©ãããã©ãŒã 察å¿ã§ãããå
éšçã«ã¯åOSåºæã®ã¯ãªããããŒãã¢ã¯ã»ã¹æ©æ§ãå©çšããŠããŸãããã®ãããOSããšã«è¥å¹²ã®æåã®éãããäŸåé¢ä¿ãååšããŸãã
Windows
Windowsç°å¢ã§ã¯ãpyperclip
ã¯OSæšæºã®APIïŒå
·äœçã«ã¯ctypes
ãéããŠWindows APIã®ã¯ãªããããŒãé¢æ°ïŒãå©çšããŸãããã®ãããè¿œå ã®ã©ã€ãã©ãªãããŒã«ãã€ã³ã¹ããŒã«ããå¿
èŠã¯ãããŸãããpip install pyperclip
ã ãã§ãããã«å©çšéå§ã§ããŸãã
macOS
macOSç°å¢ã§ã¯ãpyperclip
ã¯æšæºã§æèŒãããŠããã³ãã³ãã©ã€ã³ããŒã«pbcopy
ïŒã³ããŒçšïŒãšpbpaste
ïŒããŒã¹ãçšïŒãå©çšããŸãããããã®ããŒã«ã¯éåžžãmacOSã«ããªã€ã³ã¹ããŒã«ãããŠãããããWindowsãšåæ§ã«è¿œå ã®ã€ã³ã¹ããŒã«äœæ¥ã¯äžèŠã§ããpip install pyperclip
ã ãã§åäœããŸãã
Linux
Linuxç°å¢ã¯ãWindowsãmacOSãšæ¯ã¹ãŠå€æ§ãªãã¹ã¯ãããç°å¢ããŠã£ã³ããŠãããŒãžã£ãååšãããããã¯ãªããããŒããžã®ã¢ã¯ã»ã¹æ¹æ³ãè€æ°ãããŸããpyperclip
ã¯ä»¥äžã®åªå
é äœã§ã¯ãªããããŒãã¢ã¯ã»ã¹æ©æ§ãæ¢ããŸãã
xclip
ã³ãã³ãã©ã€ã³ãŠãŒãã£ãªãã£xsel
ã³ãã³ãã©ã€ã³ãŠãŒãã£ãªãã£gtk
/Gdk
ã¢ãžã¥ãŒã« (Python 2/3)PyQt5
/PyQt4
ã¢ãžã¥ãŒã«wl-copy
/wl-paste
ã³ãã³ãã©ã€ã³ãŠãŒãã£ãªã㣠(Waylandç°å¢åã)klipper
(KDE Plasmaç°å¢åã)
å€ãã®Linuxãã£ã¹ããªãã¥ãŒã·ã§ã³ã§ã¯ãxclip
ãŸãã¯xsel
ãæšæºã§ã¯ã€ã³ã¹ããŒã«ãããŠããªãå ŽåããããŸãããã®å Žåãpyperclip
ã䜿çšããããšãããšä»¥äžã®ãããªPyperclipException
ãçºçããŸãã
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
ãã®ãšã©ãŒãçºçããå Žåã¯ãåè¿°ã®ã€ã³ã¹ããŒã«ã»ã¯ã·ã§ã³ã§èª¬æããããã«ãxclip
ãŸãã¯xsel
ãããã±ãŒãžãããŒãžã£ã§ã€ã³ã¹ããŒã«ããŠãã ããïŒäŸïŒsudo apt-get install xclip
ïŒãäžè¬çã«xclip
ã®æ¹ãå®å®ããŠãããšãããŠããŸãã
GUIã¢ããªã±ãŒã·ã§ã³éçºãªã©ã§æ¢ã«PyQt
ãGTK
ãç°å¢ã«å°å
¥ãããŠããå Žåã¯ãããããå©çšãããããšããããŸããWaylandãã£ã¹ãã¬ã€ãµãŒãã䜿çšããŠããå Žåã¯ãwl-clipboard
ããã±ãŒãžã®ã€ã³ã¹ããŒã«ãå¿
èŠã«ãªãããšããããŸãã
ããã¹ãããŒã¿ã®ã¿å¯Ÿå¿
éèŠãªç¹ãšããŠãçŸåšã®pyperclip
ïŒããŒãžã§ã³1.9.0æç¹ïŒã¯ãã¬ãŒã³ããã¹ãïŒæåå str
ïŒã®ã³ããŒïŒããŒã¹ãã®ã¿ããµããŒãããŠããŸããç»åããã¡ã€ã«ããªããããã¹ãïŒHTML圢åŒãªã©ïŒãçŽæ¥ã³ããŒããããšã¯ã§ããŸããã
import pyperclip
# æ°åãã³ããŒããŠã...
pyperclip.copy(12345)
# ããŒã¹ããããšæååãšããŠååŸããã
pasted_data = pyperclip.paste()
print(type(pasted_data), pasted_data) # åºå: <class 'str'> 12345
# ã¯ãªããããŒãã«ç»åããŒã¿ãããå Žåãpaste()ã¯ç©ºæååãè¿ãããšãå€ã
# (äºåã«ç»åãšãã£ã¿ãªã©ã§ç»åãã³ããŒããŠãã)
image_paste_attempt = pyperclip.paste()
print(f"ç»åããŒã¿ååŸè©Šè¡: '{image_paste_attempt}'") # åºåäŸ: ç»åããŒã¿ååŸè©Šè¡: ''
ããç»åããŒã¿ãæ±ãããå Žåã¯ãPillow
ã©ã€ãã©ãªã®ImageGrab.grabclipboard()
ïŒWindows/macOSã®ã¿ïŒãªã©ãä»ã®ã©ã€ãã©ãªãæ€èšããå¿
èŠããããŸããHTML圢åŒã®ãããªãªããããã¹ããæ±ãããå Žåã¯ãWindowsã§ããã°win32clipboard
ã¢ãžã¥ãŒã«ãªã©ããã©ãããã©ãŒã åºæã®ã©ã€ãã©ãªãå©çšããå¿
èŠããããŸãã
WSL (Windows Subsystem for Linux) ç°å¢ã§ã®å©çš
WSLç°å¢ã§pyperclip
ã䜿çšããå Žåãå°ã泚æãå¿
èŠã§ããpyperclip
ããŒãžã§ã³ 1.6.1 (2018幎5æ23æ¥ãªãªãŒã¹) 以éã§ã¯WSLããµããŒãããŠããŸãããWSLç°å¢ããWindowsåŽã®ã¯ãªããããŒãã«ã¢ã¯ã»ã¹ãããããå
éšçã«ã¯Windowsã®clip.exe
ã³ãã³ããåŒã³åºãä»çµã¿ã«ãªã£ãŠããŸãã
WSLç°å¢ã§pyperclip
ã䜿ãã«ã¯ããŸãWSLã®Linuxãã£ã¹ããªãã¥ãŒã·ã§ã³å
ã«pip install pyperclip
ã§ã€ã³ã¹ããŒã«ããŸããLinuxç°å¢ã§ãããWSLã®å Žåã¯xclip
ãxsel
ã¯äžèŠã§ãã
# WSLã®ã¿ãŒããã«ã§å®è¡
pip install pyperclip
ããã§ãWSLäžã®Pythonã¹ã¯ãªããããWindowsãã¹ãã®ã¯ãªããããŒããæäœã§ããããã«ãªããŸããäŸãã°ãWSLäžã§ä»¥äžã®Pythonã¹ã¯ãªãããå®è¡ãããšãWindowsåŽã®ã¯ãªããããŒãã«ããã¹ããã³ããŒãããŸãã
# WSLäžã§å®è¡ããPythonã¹ã¯ãªãã (äŸ: wsl_clipboard_test.py)
import pyperclip
wsl_message = "ããã¯WSLããWindowsã®ã¯ãªããããŒãã«ã³ããŒãããŸããïŒ"
pyperclip.copy(wsl_message)
print("Windowsã®ã¯ãªããããŒãã«ã³ããŒããŸãããWindowsã®ã¡ã¢åž³ãªã©ã§è²Œãä»ããŠç¢ºèªããŠãã ããã")
# WindowsåŽã§ã³ããŒããå
容ãWSLã§ããŒã¹ãããããšãå¯èœ
windows_clipboard_content = pyperclip.paste()
print(f"\nWindowsã®ã¯ãªããããŒãã®å
容ãWSLã§ååŸ: {windows_clipboard_content}")
WSLç°å¢ã§ã®ã¯ãªããããŒãé£æºã¯éåžžã«äŸ¿å©ã§ããã倧éã®ããŒã¿ãé »ç¹ã«ã³ããŒïŒããŒã¹ãããå Žåãclip.exe
ã®åŒã³åºããªãŒããŒãããã«ããããã€ãã£ãç°å¢ããè¥å¹²é
ããªãå¯èœæ§ããããŸãã
å¿çšçãªäœ¿ãæ¹ãšæ©èœ âš
åºæ¬çãªcopy()
ãšpaste()
ã«å ããŠãpyperclip
ã«ã¯ã¯ãªããããŒãã®ç¶æ
ãç£èŠããããã®äŸ¿å©ãªé¢æ°ãçšæãããŠããŸãã
ã¯ãªããããŒãã空ã§ãªããªããŸã§åŸ æ© (waitForPaste())
pyperclip.waitForPaste()
é¢æ°ã¯ãã¯ãªããããŒãã空ã®å Žåãæ°ããããã¹ããã³ããŒããããŸã§ããã°ã©ã ã®å®è¡ãäžæåæ¢ããŸããã¯ãªããããŒãã«äœããã®ããã¹ããã³ããŒããããšããã®ããã¹ããè¿ããŠå®è¡ãåéããŸãã
ãããwaitForPaste()
ãåŒã³åºãããæç¹ã§æ¢ã«ã¯ãªããããŒãã«ããã¹ããååšããå Žåã¯ãåŸ
æ©ããã«å³åº§ã«ãã®ããã¹ããè¿ããŸãã
import pyperclip
import time
print("ã¯ãªããããŒããäžæŠç©ºã«ããŸãïŒç©ºæååãã³ããŒïŒ...")
pyperclip.copy('')
print("ã¯ãªããããŒãã¯ç©ºã®ã¯ãã§ãã")
print("\nwaitForPaste() ãåŒã³åºããŸãã")
print("äœãããã¹ããã³ããŒããŠãã ããïŒäŸ: ãã®æã®äžéšãCtrl+CïŒãããã°ã©ã ã¯åŸ
æ©ããŸã...")
# æ°ããããã¹ããã³ããŒããããŸã§åŸ
æ©
new_text = pyperclip.waitForPaste()
print(f"\næ°ããããã¹ããæ€åºãããŸããïŒ: '{new_text}'")
ãã®é¢æ°ã¯ããŠãŒã¶ãŒãç¹å®ã®ããã¹ããã³ããŒããã®ãåŸ ã£ãŠåŠçãéå§ãããããªã¹ã¯ãªããã§åœ¹ç«ã¡ãŸãã
ã¯ãªããããŒãã®å 容ãå€åãããŸã§åŸ æ© (waitForNewPaste())
pyperclip.waitForNewPaste()
é¢æ°ã¯ãçŸåšã®ã¯ãªããããŒãã®å
容ãšã¯ç°ãªãæ°ããããã¹ããã³ããŒããããŸã§ããã°ã©ã ã®å®è¡ãäžæåæ¢ããŸããçŸåšã®å
容ãšåããã®ãå床ã³ããŒãããŠãåŸ
æ©ãç¶ããŸãã
import pyperclip
import time
initial_text = "æåã®ããã¹ã"
print(f"ã¯ãªããããŒãã« '{initial_text}' ãã³ããŒããŸãã")
pyperclip.copy(initial_text)
print("\nwaitForNewPaste() ãåŒã³åºããŸãã")
print(f"'{initial_text}' ãšã¯ *ç°ãªã* æ°ããããã¹ããã³ããŒããŠãã ãããããã°ã©ã ã¯åŸ
æ©ããŸã...")
# çŸåšã®å
容 ('æåã®ããã¹ã') ãšã¯ç°ãªãããã¹ããã³ããŒããããŸã§åŸ
æ©
changed_text = pyperclip.waitForNewPaste()
print(f"\nã¯ãªããããŒãã®å
容ãå€åããŸããïŒ: '{changed_text}'")
ãã®é¢æ°ã¯ãã¯ãªããããŒãã®å 容ã®å€åãããªã¬ãŒãšããŠäœãåŠçãè¡ãããå Žåã«äŸ¿å©ã§ãã
ã¿ã€ã ã¢ãŠãã®èšå®
waitForPaste()
ãšwaitForNewPaste()
ã¯ãã©ã¡ããtimeout
åŒæ°ïŒç§æ°ïŒãæå®ã§ããŸããæå®ããç§æ°ãçµéããŠãã¯ãªããããŒãã«å€åããªãå ŽåãPyperclipTimeoutException
äŸå€ãçºçããŸããããã«ãããç¡éã«åŸ
ã¡ç¶ããã®ãé²ãããšãã§ããŸãã
import pyperclip
print("ã¯ãªããããŒãã®å
容ãå€åããã®ã5ç§éã ãåŸ
ã¡ãŸã...")
try:
# äºåã«äœãã³ããŒããŠãã
pyperclip.copy("åŸ
æ©åã®ããã¹ã")
print("waitForNewPaste(timeout=5) ãåŒã³åºããŸã...")
# 5ç§ä»¥å
ã«ã¯ãªããããŒãã®å
容ãå€ãããªããã°äŸå€ãçºç
new_content = pyperclip.waitForNewPaste(timeout=5)
print(f"5ç§ä»¥å
ã«å
容ãå€ãããŸãã: {new_content}")
except pyperclip.PyperclipTimeoutException:
print("ã¿ã€ã ã¢ãŠãããŸããïŒ5ç§ä»¥å
ã«ã¯ãªããããŒãã®å
容ã¯å€ãããŸããã§ããã")
ã¯ãªããããŒãã®ã¯ãªã¢
pyperclip
ã«ã¯çŽæ¥ã¯ãªããããŒãããã¯ãªã¢ãããå°çšã®é¢æ°ã¯ãããŸãããã空æåå''
ãã³ããŒããããšã§ãäºå®äžã¯ãªããããŒãã空ïŒããã¹ãããªãç¶æ
ïŒã«ããããšãã§ããŸãã
import pyperclip
# ã¯ãªããããŒãã空ã«ãã
pyperclip.copy('')
# 確èª
pasted = pyperclip.paste()
print(f"ã¯ãªã¢åŸã®ã¯ãªããããŒãã®å
容: '{pasted}'") # åºå: ã¯ãªã¢åŸã®ã¯ãªããããŒãã®å
容: ''
ãšã©ãŒãã³ããªã³ã°ãšãã©ãã«ã·ã¥ãŒãã£ã³ã° ð ïž
pyperclip
ã䜿çšããéã«ã¯ãããã€ãã®æœåšçãªåé¡ããšã©ãŒã«ééããå¯èœæ§ããããŸããé©åã«ãšã©ãŒãã³ããªã³ã°ãè¡ãããšã§ãããå
ç¢ãªã¹ã¯ãªãããäœæã§ããŸãã
äž»ãªäŸå€ã¯ã©ã¹
pyperclip
ãçºè¡ããå¯èœæ§ã®ããäž»ãªäŸå€ã¯ä»¥äžã®éãã§ãã
pyperclip.PyperclipException
:pyperclip
ã«é¢é£ããäžè¬çãªãšã©ãŒã®åºåºã¯ã©ã¹ãpyperclip.PyperclipWindowsException
: Windowsåºæã®ã¯ãªããããŒãæäœãšã©ãŒãpyperclip.PyperclipMacException
: macOSåºæã®ã¯ãªããããŒãæäœãšã©ãŒïŒéåžžã¯pbcopy
/pbpaste
ã³ãã³ãã®ãšã©ãŒïŒãpyperclip.PyperclipLinuxException
: Linuxåºæã®ã¯ãªããããŒãæäœãšã©ãŒïŒéåžžã¯xclip
/xsel
çã®ãšã©ãŒïŒãpyperclip.PyperclipTimeoutException
:waitForPaste()
ãŸãã¯waitForNewPaste()
ãã¿ã€ã ã¢ãŠãããå Žåã«çºçã
ç¹ã«æ³šæãã¹ãã¯ãLinuxç°å¢ã§xclip
ãxsel
ãèŠã€ãããªãå Žåã«çºçããPyperclipException
ã§ããããã¯ããã©ãããã©ãŒã ããšã®éããšæ³šæç¹ãã§èª¬æãã察åŠæ³ãå¿
èŠã§ãã
import pyperclip
try:
# äœããã®ã¯ãªããããŒãæäœ
content = pyperclip.paste()
print(f"ã¯ãªããããŒãã®å
容: {content}")
pyperclip.copy("ãšã©ãŒãã³ããªã³ã°ã®ãã¹ã")
print("ã³ããŒæå")
except pyperclip.PyperclipException as e:
print(f"Pyperclipãšã©ãŒãçºçããŸãã: {e}")
print("Linuxç°å¢ã®å ŽåãxclipãŸãã¯xselãã€ã³ã¹ããŒã«ãããŠããã確èªããŠãã ããã")
# ããã§ä»£æ¿åŠçããŠãŒã¶ãŒãžã®éç¥ãè¡ã
except Exception as e:
print(f"äºæãã¬ãšã©ãŒãçºçããŸãã: {e}")
ããããåé¡ãšè§£æ±ºç
åé¡ | åå | 解決ç |
---|---|---|
ModuleNotFoundError: No module named 'pyperclip' | pyperclip ãã€ã³ã¹ããŒã«ãããŠããªãããŸãã¯Pythonã®å®è¡ç°å¢ïŒä»®æ³ç°å¢ãªã©ïŒãç°ãªãã | pip install pyperclip ãå®è¡ãããè€æ°ã®Pythonç°å¢ãããå Žåã¯ã䜿çšããŠããç°å¢ã«æ£ããã€ã³ã¹ããŒã«ãããŠããã確èªããïŒäŸ: pip show pyperclip ãpython -m pip show pyperclip ïŒã |
Linuxã§PyperclipException: Pyperclip could not find a copy/paste mechanism... ãšã©ãŒ | xclip , xsel , wl-clipboard çã®å¿
èŠãªå€éšããŒã«ãã€ã³ã¹ããŒã«ãããŠããªãã | 䜿çšããŠãããã£ã¹ããªãã¥ãŒã·ã§ã³ã®ããã±ãŒãžãããŒãžã£ã§xclip ïŒæšå¥šïŒãŸãã¯xsel ãã€ã³ã¹ããŒã«ããïŒäŸ: sudo apt-get install xclip ïŒãWaylandç°å¢ã®å Žåã¯wl-clipboard ãã€ã³ã¹ããŒã«ããã |
paste() ã空æåå'' ãè¿ã | ã¯ãªããããŒããå®éã«ç©ºã§ããããããã¹ã以å€ã®ããŒã¿ïŒç»åããã¡ã€ã«çïŒãã³ããŒãããŠããã | ã¯ãªããããŒãã®å
容ã確èªãããpyperclip ã¯ããã¹ãã®ã¿å¯Ÿå¿ããŠãããããæå³ããããã¹ãããŒã¿ãã³ããŒãããŠããã確èªããã |
ã³ããŒããã¯ãã®æ¥æ¬èªããã¹ããæååãããïŒçšãªã±ãŒã¹ïŒ | OSãã¿ãŒããã«ã®ãšã³ã³ãŒãã£ã³ã°èšå®ã®åé¡ããŸãã¯å€ãããŒãžã§ã³ã®pyperclip ã®åé¡ã®å¯èœæ§ã | OSãã¿ãŒããã«ãPythonã¹ã¯ãªããã®ãšã³ã³ãŒãã£ã³ã°ãUTF-8ã«çµ±äžãããpyperclip ãææ°ããŒãžã§ã³ã«ã¢ããããŒããã (pip install --upgrade pyperclip )ã |
WSLç°å¢ã§åäœããªã | å€ãããŒãžã§ã³ã®pyperclip ã䜿çšããŠãã (1.6.1æªæº)ããŸãã¯WindowsåŽã®clip.exe ãžã®ãã¹ãéã£ãŠããªãïŒéåžžã¯åé¡ãªãã¯ãïŒã | pyperclip ãææ°ããŒãžã§ã³ã«ã¢ããããŒãããã |
ã»ãã¥ãªãã£ã«é¢ããèæ ®äºé ð
ã¯ãªããããŒãã¯ããŠãŒã¶ãŒãæå³çã«ã³ããŒãããã¹ã¯ãŒããå人æ
å ±ãªã©ãæ©å¯æ§ã®é«ãæ
å ±ãå«ãå¯èœæ§ããããŸããpyperclip.paste()
ã䜿çšããã¹ã¯ãªããã¯ãåççã«ã¯ãããã®æ
å ±ã«ã¢ã¯ã»ã¹ã§ããŠããŸããŸãã
äžç¹å®å€æ°ã®ãŠãŒã¶ãŒãå©çšããå¯èœæ§ã®ããã¢ããªã±ãŒã·ã§ã³ããä¿¡é Œã§ããªãå€éšã®å ¥åïŒäŸïŒãŠã§ãããååŸããããŒã¿ïŒãã¯ãªããããŒãã«ã³ããŒãããããªã¹ã¯ãªãããäœæããéã«ã¯ãã»ãã¥ãªãã£ãªã¹ã¯ãèæ ®ããå¿ èŠããããŸãã
- ã¹ã¯ãªãããæå³ããæ©å¯æ å ±ãã¯ãªããããŒãããèªã¿åããªãããã«æ³šæããã
- ãŠãŒã¶ãŒã«ã¯ãªããããŒãã®å 容ãäžæžãããå¯èœæ§ãããããšãæ瀺ããã
- ç¹ã«
waitForPaste()
ãwaitForNewPaste()
ã䜿çšããå Žåãããã¯ã°ã©ãŠã³ãã§åäœããããšãŠãŒã¶ãŒãæå³ããªãã¿ã€ãã³ã°ã§æ©å¯æ å ±ãååŸããŠããŸããªã¹ã¯ããªããæ€èšããã
pyperclip
èªäœã«è匱æ§ãããããã§ã¯ãããŸããããã¯ãªããããŒããšããå
±æãªãœãŒã¹ãæ±ãæ§è³ªäžãå©çšæ¹æ³ã«ã¯æ³šæãå¿
èŠã§ãã
ãŠãŒã¹ã±ãŒã¹ãšå®è·µäŸ ð¡
pyperclip
ã®ã·ã³ãã«ãã¯ãæ§ã
ãªèªååã¿ã¹ã¯ã䟿å©ããŒã«ã®äœæã«åœ¹ç«ã¡ãŸããããã§ã¯ããã€ãã®å
·äœçãªãŠãŒã¹ã±ãŒã¹ãšç°¡åãªã³ãŒãäŸã玹ä»ããŸãã
1. å®åæã®ç°¡åã³ããŒ
ã¡ãŒã«ã®çœ²åããã䜿ãã³ãŒãã¹ãããããç¹å®ã®ãã©ãŒãããã®ããã¹ããªã©ãé »ç¹ã«å©çšããå®åæãã¯ãªããããŒãã«çŽ æ©ãã³ããŒããã¹ã¯ãªãããäœæã§ããŸãã
import pyperclip
import sys
TEMPLATES = {
"greeting": "ãã€ããäžè©±ã«ãªã£ãŠãããŸãã\næ ªåŒäŒç€Ÿããã®â³â³ã§ãã",
"closing": "äœåãããããé¡ãç³ãäžããŸãã",
"code_header": "# -*- coding: utf-8 -*-"
}
if len(sys.argv) < 2 or sys.argv[1] not in TEMPLATES:
print("䜿ãæ¹: python copy_template.py [ããŒ]")
print("å©çšå¯èœãªããŒ:", list(TEMPLATES.keys()))
sys.exit(1)
key = sys.argv[1]
text_to_copy = TEMPLATES[key]
pyperclip.copy(text_to_copy)
print(f"'{key}' ã®å®åæãã¯ãªããããŒãã«ã³ããŒããŸããïŒ")
ãããcopy_template.py
ãšããŠä¿åããã¿ãŒããã«ããpython copy_template.py greeting
ã®ããã«å®è¡ããã°ã察å¿ããå®åæãã¯ãªããããŒãã«å
¥ããŸãã
2. ã¯ãªããããŒãã®å 容ãå å·¥
ã¯ãªããããŒãã«ã³ããŒãããããã¹ããå å·¥ããŠãå床ã¯ãªããããŒãã«æ»ãããŒã«ãç°¡åã«äœããŸããäŸãã°ãã³ããŒããããã¹ãããã¹ãŠå€§æåã«å€æããã¹ã¯ãªããã§ãã
import pyperclip
try:
original_text = pyperclip.paste()
if not original_text:
print("ã¯ãªããããŒãã«ããã¹ãããããŸããã")
else:
uppercase_text = original_text.upper()
pyperclip.copy(uppercase_text)
print("ã¯ãªããããŒãã®ããã¹ãã倧æåã«å€æããŠåã³ããŒããŸããïŒ")
print(f"å€æåŸ: {uppercase_text}")
except pyperclip.PyperclipException as e:
print(f"ãšã©ãŒ: {e}")
ãã®ã¹ã¯ãªãããå®è¡ãããšãçŸåšã¯ãªããããŒãã«ããããã¹ãã倧æåã«å€æãããã¯ãªããããŒãã®å 容ãæŽæ°ãããŸãã
3. Webã¹ã¯ã¬ã€ãã³ã°çµæã®å ±æ
WebããŒãžããç¹å®ã®æ
å ±ãæœåºãããã®çµæãã¯ãªããããŒãã«ã³ããŒããŠä»ã®ã¢ããªã±ãŒã·ã§ã³ã§å©çšããããšãã£ãé£æºãå¯èœã§ããäŸãã°ãrequests
ãšBeautifulSoup4
ã䜿ã£ãŠããŒãžã®ã¿ã€ãã«ãååŸããã³ããŒããäŸã§ãã
import pyperclip
import requests
from bs4 import BeautifulSoup
import sys
# requests, beautifulsoup4 ãå¿
èŠ: pip install requests beautifulsoup4
if len(sys.argv) < 2:
print("䜿ãæ¹: python get_title.py [URL]")
sys.exit(1)
url = sys.argv[1]
try:
response = requests.get(url, timeout=10)
response.raise_for_status() # ãšã©ãŒãã§ãã¯
response.encoding = response.apparent_encoding # æååã察ç
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.string if soup.title else "ã¿ã€ãã«ãèŠã€ãããŸãã"
pyperclip.copy(title.strip())
print(f"ããŒãžã®ã¿ã€ãã«ãã¯ãªããããŒãã«ã³ããŒããŸãã:\n'{title.strip()}'")
except requests.exceptions.RequestException as e:
print(f"URLãžã®ã¢ã¯ã»ã¹äžã«ãšã©ãŒãçºçããŸãã: {e}")
except Exception as e:
print(f"äºæãã¬ãšã©ãŒãçºçããŸãã: {e}")
python get_title.py https://www.python.org
ã®ããã«å®è¡ãããšãæå®ããURLã®ããŒãžã®ã¿ã€ãã«ãã¯ãªããããŒãã«ã³ããŒãããŸãã
4. è€æ°è¡ããã¹ãã®äžæ¬ã³ããŒ
ãªã¹ãããã¡ã€ã«ããèªã¿èŸŒãã è€æ°è¡ã®ããã¹ãããæ¹è¡ãå«ãã ãŸãŸã¯ãªããããŒãã«ã³ããŒã§ããŸãã
import pyperclip
lines = [
"ããã¯1è¡ç®ã§ãã",
"ããã¯2è¡ç®ã§ãã",
"ãããŠãããã3è¡ç®ã§ãã"
]
# ãªã¹ãã®åèŠçŽ ãæ¹è¡æåã§çµåããŠã³ããŒ
multi_line_text = "\n".join(lines)
pyperclip.copy(multi_line_text)
print("è€æ°è¡ã®ããã¹ããã¯ãªããããŒãã«ã³ããŒããŸããã")
print("--- ã³ããŒãããå
容 ---")
print(pyperclip.paste())
print("----------------------")
ãããã®äŸã¯ã»ãã®äžéšã§ããpyperclip
ãä»ã®Pythonã©ã€ãã©ãªïŒãã¡ã€ã«æäœãããŒã¿åŠçãGUIããŒã«ããããªã©ïŒãšçµã¿åãããããšã§ãããã«é«åºŠã§äŸ¿å©ãªèªååããŒã«ãéçºã§ããŸãã
代æ¿æ段ãšæ¯èŒ ð€
Pythonã§ã¯ãªããããŒããæäœããæ¹æ³ã¯pyperclip
ã ãã§ã¯ãããŸãããããã€ãã®ä»£æ¿æ段ãšæ¯èŒããŠã¿ãŸãããã
æ¹æ³ | ç¹åŸŽ | ã¡ãªãã | ãã¡ãªãã |
---|---|---|---|
pyperclip | ã¯ãã¹ãã©ãããã©ãŒã ãã·ã³ãã«ãããã¹ãå°çš | ã»éåžžã«ç°¡å ã»OSéã®å·®ç°ãåžå ã»å€éšäŸåãå°ãªã(Win/Mac) ã»WSLå¯Ÿå¿ | ã»ããã¹ãã®ã¿å¯Ÿå¿ ã»Linuxã§å€éšããŒã«äŸå ã»é«åºŠãªæ©èœã¯å°ãªã |
Tkinter (tkinter.Tk().clipboard_get() / .clipboard_clear() / .clipboard_append() ) | Pythonæšæºã©ã€ãã©ãªãGUIãã¬ãŒã ã¯ãŒã¯ã®äžéš | ã»è¿œå ã€ã³ã¹ããŒã«äžèŠ ã»æšæºæ©èœã§å®å¿ | ã»Tkinterã®ãŠã£ã³ããŠãïŒé衚瀺ã§ãïŒåæåããå¿
èŠããã ã»ã³ãŒããããåé·ã«ãªã ã»ããã¹ãäžå¿ |
PyQt / PySide (QApplication.clipboard() ) | é«æ©èœãªGUIãã¬ãŒã ã¯ãŒã¯ã®äžéš | ã»ããã¹ã以å€ïŒç»åãMIMEã¿ã€ãæå®ïŒãæ±ãã ã»é«æ©èœ | ã»ã©ã€ãã©ãªãµã€ãºã倧ãã ã»äŸåé¢ä¿ãå€ã ã»ã¯ãªããããŒãæäœã ãã«ã¯å€§ãã |
platform + subprocess | OSãå€å¥ããpbcopy /pbpaste (Mac), clip.exe (Win), xclip /xsel (Linux) ãçŽæ¥åŒã³åºã | ã»å€éšã©ã€ãã©ãªäžèŠïŒäŸåããŒã«ã¯å¿
èŠïŒ ã»æåã现ããå¶åŸ¡å¯èœ | ã»OSããšã®åå²åŠçãèªåã§æžãå¿
èŠããã ã»ãšã©ãŒãã³ããªã³ã°ãè€é ã»ä¿å®æ§ãäœäžãããã |
pywin32 (Windows) | Windows APIãžã®ã¢ã¯ã»ã¹ãæäŸ | ã»Windowsã®ã¯ãªããããŒãæ©èœããã«æŽ»çšå¯èœïŒå€æ§ãªãã©ãŒãããïŒ | ã»Windowså°çš ã»åŠç¿ã³ã¹ããããé«ã |
pyclip (pyperclip3ã®åŸç¶) | pyperclipã®ãã©ãŒã¯ããã€ããªããŒã¿å¯Ÿå¿ãè©Šã¿ãŠãã | ã»ããã¹ã以å€ã®ããŒã¿ãæ±ããå¯èœæ§ããã (éçºç¶æ³ã«ãã) | ã»pyperclipã»ã©åºã䜿ãããŠããªã ã»å®å®æ§ãäºææ§ã¯èŠç¢ºèª (éçºã¯æŽ»çºã§ã¯ãªãæ§å) |
çµè«ãšããŠïŒ
- åçŽãªããã¹ãã®ã³ããŒïŒããŒã¹ããã¯ãã¹ãã©ãããã©ãŒã ã§æ軜ã«è¡ãããå Žåã¯ã
pyperclip
ãæé©ã§ãããã®ã·ã³ãã«ããšå°å ¥ã®æ軜ãã¯å€§ããªå©ç¹ã§ãã - Pythonæšæºã©ã€ãã©ãªã ãã§å®çµããããããããã¯ç°¡åãªGUIã¢ããªã®äžéšãšããŠäœ¿ããªãã
Tkinter
ãéžæè¢ã«ãªããŸãã - ããã¹ã以å€ã®ããŒã¿ïŒç»åãªã©ïŒãæ±ãããããŸãã¯é«æ©èœãªGUIã¢ããªãéçºããŠãããªãã
PyQt
/PySide
ãé©ããŠããŸãã - Windowså°çšã§é«åºŠãªã¯ãªããããŒãæäœãå¿
èŠãªã
pywin32
ã匷åã§ãã - äŸåé¢ä¿ã極åæžããããããŸãã¯OSããšã®æåãå®å
šã«å¶åŸ¡ãããäžçŽè
ã§ããã°ã
subprocess
ã§åOSã®ã³ãã³ããçŽæ¥å©ãæ¹æ³ããããŸãããè€éæ§ãå¢ããŸãã
å€ãã®äžè¬çãªãŠãŒã¹ã±ãŒã¹ã§ã¯ãpyperclip
ã®æ軜ããšã¯ãã¹ãã©ãããã©ãŒã æ§ããæããã©ã³ã¹ã®åããéžæè¢ãšãªãã§ãããã
ãŸãšãïŒpyperclipã§äœæ¥ãå¹çåãããïŒ ð
ãã®èšäºã§ã¯ãPythonã®ã¯ãªããããŒãæäœã©ã€ãã©ãªpyperclip
ã«ã€ããŠãã€ã³ã¹ããŒã«æ¹æ³ããåºæ¬çãªäœ¿ãæ¹ããã©ãããã©ãŒã ããšã®æ³šæç¹ãå¿çšçãªæ©èœããšã©ãŒãã³ããªã³ã°ããããŠå
·äœçãªãŠãŒã¹ã±ãŒã¹ãŸã§ãå¹
åºã解説ããŸããã
pyperclip
ã®äž»ãªã¡ãªããïŒ
- ã¯ãã¹ãã©ãããã©ãŒã : Windows, macOS, Linux, WSLã§åãã³ãŒããåäœã
- ã·ã³ãã«:
copy()
ãšpaste()
ã ãã§åºæ¬çãªæäœãå®çµãéåžžã«åŠç¿ããããã - 軜é: äŸåé¢ä¿ãå°ãªãïŒç¹ã«Windows/macOSïŒã
- 䟿å©é¢æ°:
waitForPaste()
,waitForNewPaste()
ã§ã¯ãªããããŒãã®ç£èŠãå¯èœã
äžæ¹ã§ãããã¹ãããŒã¿ããæ±ããªãç¹ããLinuxç°å¢ã§ã®å€éšããŒã«äŸåãšãã£ãå¶éããããŸãããæ¥åžžçãªããã¹ãããŒã¹ã®ã³ããŒïŒããŒã¹ãèªååã«ãããŠã¯ãéåžžã«åŒ·åã§äŸ¿å©ãªããŒã«ã§ãã
åçŽäœæ¥ã®èªååãã¹ã¯ãªããéã®ããŒã¿é£æºãéçºæ¯æŽããŒã«ã®äœæãªã©ãpyperclip
ã掻çšã§ããå Žé¢ã¯å€å²ã«ããããŸãããã²ãããªãã®Pythonãããžã§ã¯ããæ¥ã
ã®äœæ¥ã«åãå
¥ããŠãå¹çåãå³ã£ãŠã¿ãŠãã ããïŒ
Happy coding! ðâïžð
ã³ã¡ã³ã