Python - pyperclip 模組



在 Python 中,pyperclip 模組用於各種與剪貼簿相關的操作,例如剪下、複製和貼上。它非常適合需要在應用程式、指令碼或涉及文字操作的多個程序之間傳輸資料的情況。

在本教程中,我們將學習 **pyperclip** 模組的安裝、其用途以及如何透過示例使用它。

什麼是 pyperclip 模組?為什麼需要它?

**pyperclip** 模組對於自動化涉及剪貼簿的文字處理非常重要。您可以將資料從指令碼複製到剪貼簿,然後輕鬆地貼上到任何文件、瀏覽器或任何應用程式中。同樣,您無需手動複製您已放入剪貼簿的內容並將其貼上到指令碼中。

pyperclip 模組的用例包括:

  • 自動將指令碼結果複製到其他應用程式。
  • 自動化需要重複手動複製貼上的任務。
  • 建立用於操作文字的工具或實用程式。

使用 PIP 安裝 pyperclip 模組

您可以透過 pip 安裝 pyperclip 模組,以下是安裝命令:

pip install pyperclip

將文字複製到剪貼簿

要將特定文字複製到剪貼簿,可以使用 pyperclip.copy() 方法。此函式接受字串作為輸入,並將其直接傳輸到系統的剪貼簿。複製後,文字可用於貼上到任何支援剪貼簿操作的應用程式中。

示例

此示例演示如何將文字複製到剪貼簿:

import pyperclip as pc
def transfer_to_buffer(info):
   """Transfers the given info to the system's clipboard."""
   pc.copy(info)
   print("Data has been transferred to the clipboard!")

# The message to be copied to the clipboard
buffer_data = "Clipboard operation successful!"
# Executing the transfer
transfer_to_buffer(buffer_data)

用於複製和貼上文字的模組化函式

您可以將剪貼簿功能包裝在模組化函式中,以使操作更有條理和可重用。我們可以使用函式快速將文字複製到剪貼簿或使用 **pyperclip.paste()** 從剪貼簿中檢索它。

示例

此示例演示了上述概念:

import pyperclip as pc
def fetch_from_buffer():
   """Fetches and returns data from the system's clipboard."""
   return pc.paste()
# Retrieve data from clipboard
retrieved_data = fetch_from_buffer()
print("Data fetched from clipboard:", retrieved_data)

使用模組化函式複製和貼上文字

這兩種方法都過載了行為,其中包括一個工作流程,可以快速將某些內容複製到剪貼簿,然後在其他程式碼或文件中獲取該內容。

示例

import pyperclip as pc
def transfer_to_buffer(data):
   # Transfers the given data to the clipboard.
   pc.copy(data)
   print("Data has been transferred to the clipboard!")
def retrieve_from_buffer():
   # Fetches and returns the current content of the clipboard.
   return pc.paste()
# Data to transfer to the clipboard
data_for_transfer = "This data was transferred using the buffer."
# Transfer the data to the clipboard
transfer_to_buffer(data_for_transfer)
# Retrieve the data from the clipboard
buffer_content = retrieve_from_buffer()
# Display the retrieved data from the clipboard
print(f"Data fetched from clipboard: {buffer_content}")

建立 .txt 檔案並將文字從剪貼簿貼上到檔案中

在這裡,我們將建立一個程式,該程式能夠將剪貼簿的內容儲存到 .txt 檔案中。我們將使用寫入模式 ('w') 的 **open()** 函式來建立或覆蓋文字檔案,並將檢索到的剪貼簿內容寫入該檔案。此功能用於儲存剪貼簿內容以供將來參考或記錄。

示例

import pyperclip as pc

def transfer_to_buffer(data):
   """Transfers the given data to the clipboard."""
   pc.copy(data)
   print("Data has been transferred to the clipboard!")

def retrieve_from_buffer():
   """Fetches and returns the current content of the clipboard."""
   return pc.paste()

def save_data_to_file(file_name, data):
   """Saves the provided data to a file."""
   with open(file_name, 'w') as file:
      file.write(data)
   print(f"Data saved to {file_name}.")

# Text to copy to the clipboard
data_for_transfer = "This data was transferred using the buffer."

# Copy the text to the clipboard
transfer_to_buffer(data_for_transfer)

# Retrieve the text from the clipboard
buffer_content = retrieve_from_buffer()

# Display the text retrieved from the clipboard
print(f"Data fetched from clipboard: {buffer_content}")

# Save the retrieved text to a .txt file
file_name = 'output.txt'
save_data_to_file(file_name, buffer_content)

輸出

clipboard

**檢視我們的示例 4 程式碼**,這段程式碼將在我們的電腦路徑中自動建立一個 output.txt 檔案,您編寫的程式碼將被複制並貼上到該txt檔案中。

clipboard 1
python_reference.htm
廣告