Windows 10下如何用Python实现文件复制到剪贴板(支持Ctrl+V)
解决方案:Python实现文件复制到剪贴板并通过Selenium自动化Google Lens粘贴
一、跨平台文件复制到剪贴板
复制文件到系统剪贴板的实现因操作系统而异,以下是各平台的具体代码:
Windows 平台
通过ctypes调用Windows API,处理剪贴板的CF_HDROP格式(用于文件拖拽/复制):
import ctypes import os from ctypes import wintypes CF_HDROP = 15 class DROPFILES(ctypes.Structure): _fields_ = [("pFiles", wintypes.DWORD), ("x", wintypes.LONG), ("y", wintypes.LONG), ("fNC", wintypes.BOOL), ("fWide", wintypes.BOOL)] def copy_file_to_clipboard(file_path): file_path = str(file_path) if not os.path.exists(file_path): raise FileNotFoundError(f"文件 {file_path} 不存在") ctypes.windll.user32.OpenClipboard(None) ctypes.windll.user32.EmptyClipboard() dropfiles = DROPFILES() dropfiles.pFiles = ctypes.sizeof(DROPFILES) dropfiles.fWide = True # 使用Unicode路径 # 构造剪贴板数据:DROPFILES结构 + UTF-16LE编码的路径 + 双空字符结束符 data_size = ctypes.sizeof(DROPFILES) + len(file_path)*2 + 2 data = (ctypes.c_char * data_size)() ctypes.memmove(data, ctypes.addressof(dropfiles), ctypes.sizeof(DROPFILES)) ctypes.memmove(data + ctypes.sizeof(DROPFILES), file_path.encode('utf-16le'), len(file_path)*2) h_global = ctypes.windll.kernel32.GlobalAlloc(0x40, data_size) h_global_lock = ctypes.windll.kernel32.GlobalLock(h_global) ctypes.memmove(h_global_lock, data, data_size) ctypes.windll.kernel32.GlobalUnlock(h_global) ctypes.windll.user32.SetClipboardData(CF_HDROP, h_global) ctypes.windll.user32.CloseClipboard()
macOS 平台
通过osascript调用AppleScript实现文件复制:
import subprocess def copy_file_to_clipboard(file_path): file_path = str(file_path) script = f''' set theFile to POSIX file "{file_path}" set the clipboard to theFile ''' subprocess.run(['osascript', '-e', script], check=True)
Linux 平台
依赖xclip工具(需先通过sudo apt install xclip安装):
import subprocess def copy_file_to_clipboard(file_path): file_path = str(file_path) subprocess.run( ['xclip', '-selection', 'clipboard', '-target', 'text/uri-list', '-i', file_path], check=True )
二、Selenium自动化Google Lens粘贴文件
Google Lens的页面结构可能随版本更新,以下两种方法可按需选择:
方法1:直接上传文件(推荐)
定位页面的文件上传<input>元素,直接传入文件路径,比模拟粘贴更稳定:
from selenium import webdriver from selenium.webdriver.common.by import By import os import time # 初始化浏览器 driver = webdriver.Chrome() driver.get('https://lens.google.com/') time.sleep(2) # 等待页面加载完成 # 点击上传按钮(定位器需根据页面实际结构调整) upload_btn = driver.find_element(By.XPATH, '//button[@aria-label="上传图片"]') upload_btn.click() time.sleep(1) # 定位隐藏的文件上传input并传入文件路径 file_input = driver.find_element(By.XPATH, '//input[@type="file"]') target_file = os.path.abspath('your_image.jpg') # 替换为你的文件路径 file_input.send_keys(target_file) # 后续处理识别结果...
方法2:模拟Ctrl+V粘贴
若必须使用粘贴操作,需先将文件复制到剪贴板,再模拟快捷键:
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys import time # 先复制文件到剪贴板(根据系统调用对应函数) copy_file_to_clipboard('your_image.jpg') driver = webdriver.Chrome() driver.get('https://lens.google.com/') time.sleep(2) # 激活粘贴区域(定位器需根据页面调整) paste_area = driver.find_element(By.XPATH, '//div[@role="button" and contains(text(), "粘贴图片")]') paste_area.click() time.sleep(1) # 模拟Ctrl+V(macOS替换为Keys.COMMAND) actions = ActionChains(driver) actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform() # 后续处理识别结果...
注意事项
- 确保Selenium驱动版本与浏览器版本完全匹配,避免兼容性问题
- Google Lens的页面元素定位器可能随更新变化,需定期检查并调整
- macOS/Linux系统可能需要授予终端/IDE访问剪贴板的权限
- Linux平台需提前安装
xclip工具,否则剪贴板操作会失败
内容的提问来源于stack exchange,提问作者mrithul e
相关产品推荐
相关产品推荐

