使用 CopyQ 导出剪贴板历史到 TXT 文件

学习笔记作者:admin日期:2025-05-28点击:19

摘要:通过 Python 脚本结合 CopyQ 命令行工具,将剪贴板历史中的所有内容导出到一个 .txt 文件。

使用 CopyQ 导出剪贴板历史到 TXT 文件

功能说明

我们将使用 CopyQ 命令行工具:

  • copyq eval-script "read(all())":读取所有剪贴板历史项。
  • 然后将输出保存到一个 .txt 文件中。

示例脚本(Python)

import os
from subprocess import check_output, CalledProcessError
import datetime

# 获取当前时间用于文件命名
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
output_file = f"clipboard_history_{timestamp}.txt"

try:
    # 使用 copyq 读取所有剪贴板条目
    result = check_output(["copyq", "eval-script", "read(all())"], universal_newlines=True)

    # 将结果写入文件
    with open(output_file, "w", encoding="utf-8") as f:
        f.write(result)

    print(f"✅ 剪贴板历史已保存到文件: {output_file}")

except CalledProcessError as e:
    print("❌ 执行 copyq 命令失败,请确保 CopyQ 已安装并在 PATH 中。")
except Exception as e:
    print(f"⚠️ 出现错误: {e}")

使用方法

1. 确保你已经安装并运行了 CopyQ
2. 确保 copyq 命令在终端中可以直接执行:copyq --version
3. 将上面的代码保存为 export_clipboard.py。
4. 在终端中运行:python export_clipboard.py

输出示例文件内容(部分)

Text: Hello World! Text: https://example.com/ Image: <Binary data - size: 12345 bytes>

> 注:图片等二进制内容会显示为 <Binary data ...>, 无法直接查看图像。

可选增强功能

- 过滤只导出文本内容。
- 自动打开生成的 .txt 文件。
- 指定导出路径。
- 添加 GUI 界面选择保存位置。

上一篇      下一篇