右键双击自动保存剪贴板内容到markdown文件
This commit is contained in:
parent
9918eddc49
commit
c5850d67fd
1
project/llmclipboard/.python-version
Normal file
1
project/llmclipboard/.python-version
Normal file
@ -0,0 +1 @@
|
||||
3.10
|
||||
8
project/llmclipboard/README.md
Normal file
8
project/llmclipboard/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
Prompt
|
||||
我的目标是:写一个Python程序,可以通过选择任何windows应用的一段富文本,点击鼠标右键后弹出一个选项,点击这个选项可以把富文本保存在配置路径的.markdown文件里,这个markdown会保持选中富文本的图文等相对样式不变可以正常显示。请给出完整的实现步骤和代码,应用.
|
||||
|
||||
以管理员权限打开命令提示符,进入脚本所在目录,运行:
|
||||
python app.py --install
|
||||
|
||||
如果要卸载右键菜单,运行:
|
||||
python app.py --uninstall
|
||||
139
project/llmclipboard/app.py
Normal file
139
project/llmclipboard/app.py
Normal file
@ -0,0 +1,139 @@
|
||||
import os
|
||||
import time
|
||||
import configparser
|
||||
from pynput import mouse
|
||||
import win32clipboard
|
||||
import win32con
|
||||
from html2text import html2text
|
||||
import keyboard
|
||||
import threading
|
||||
import sys
|
||||
import logging
|
||||
|
||||
class TextCaptureApp:
|
||||
def __init__(self):
|
||||
self.load_config()
|
||||
self.running = True
|
||||
self.last_right_click_time = 0
|
||||
self.setup_logging()
|
||||
|
||||
def load_config(self):
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
self.double_click_threshold = config.getfloat('Settings', 'double_click_threshold', fallback=0.3)
|
||||
self.save_location = config.get('Settings', 'save_location', fallback=os.path.join(os.path.expanduser('~'), 'Desktop'))
|
||||
self.copy_delay = config.getfloat('Settings', 'copy_delay', fallback=0.1)
|
||||
|
||||
def setup_logging(self):
|
||||
logging.basicConfig(
|
||||
filename='text_capture.log',
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
def save_to_markdown(self, content):
|
||||
try:
|
||||
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
||||
file_path = os.path.join(self.save_location, f'captured_text_{timestamp}.md')
|
||||
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
self.logger.info(f"Content saved to: {file_path}")
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error saving file: {e}")
|
||||
|
||||
def get_clipboard_content(self):
|
||||
try:
|
||||
win32clipboard.OpenClipboard()
|
||||
|
||||
try:
|
||||
content = win32clipboard.GetClipboardData(win32con.CF_HTML)
|
||||
self.logger.info("Clipboard HTML content captured")
|
||||
return html2text(content)
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
content = win32clipboard.GetClipboardData(win32con.CF_TEXT)
|
||||
self.logger.info("Clipboard TEXT content captured")
|
||||
return content.decode('gbk')
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
content = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
|
||||
self.logger.info("Clipboard UNICODE TEXT content captured")
|
||||
return content
|
||||
except:
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error getting clipboard content: {e}")
|
||||
return None
|
||||
finally:
|
||||
try:
|
||||
win32clipboard.CloseClipboard()
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error closing clipboard: {e}")
|
||||
|
||||
def simulate_copy(self):
|
||||
keyboard.press('ctrl')
|
||||
keyboard.press('c')
|
||||
keyboard.release('c')
|
||||
keyboard.release('ctrl')
|
||||
time.sleep(self.copy_delay) # 等待复制操作完成
|
||||
|
||||
def on_click(self, x, y, button, pressed):
|
||||
if not pressed:
|
||||
return
|
||||
|
||||
if button == mouse.Button.right:
|
||||
current_time = time.time()
|
||||
if (current_time - self.last_right_click_time) < self.double_click_threshold:
|
||||
self.simulate_copy()
|
||||
content = self.get_clipboard_content()
|
||||
if content:
|
||||
self.save_to_markdown(content)
|
||||
else:
|
||||
self.logger.warning("No content captured from clipboard")
|
||||
self.last_right_click_time = current_time
|
||||
|
||||
def on_keyboard_event(self, event):
|
||||
# 监听Esc键退出程序
|
||||
if event.name == 'esc':
|
||||
self.running = False
|
||||
return False
|
||||
|
||||
def run(self):
|
||||
self.logger.info("Text Capture App started")
|
||||
print("文本捕获程序已启动...")
|
||||
print("使用说明:")
|
||||
print("1. 选中要保存的文本")
|
||||
print("2. 快速双击鼠标右键来保存选中的文本")
|
||||
print("3. 按ESC键退出程序")
|
||||
|
||||
mouse_listener = mouse.Listener(on_click=self.on_click)
|
||||
mouse_listener.start()
|
||||
|
||||
# 启动键盘监听
|
||||
keyboard.on_press(self.on_keyboard_event)
|
||||
|
||||
# Wait until the running flag becomes False
|
||||
while self.running:
|
||||
time.sleep(0.1)
|
||||
|
||||
print("\n程序已退出")
|
||||
mouse_listener.stop()
|
||||
mouse_listener.join()
|
||||
keyboard_listener.stop()
|
||||
keyboard_listener.join()
|
||||
self.logger.info("Text Capture App stopped")
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
app = TextCaptureApp()
|
||||
app.run()
|
||||
except Exception as e:
|
||||
logging.error(f"程序发生错误: {e}")
|
||||
input("按Enter键退出...")
|
||||
3
project/llmclipboard/config.ini
Normal file
3
project/llmclipboard/config.ini
Normal file
@ -0,0 +1,3 @@
|
||||
[Settings]
|
||||
double_click_threshold = 0.3
|
||||
save_location = F:\BaiduSyncdisk\note\NoteForZhukang\000 inbox
|
||||
7
project/llmclipboard/pyproject.toml
Normal file
7
project/llmclipboard/pyproject.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[project]
|
||||
name = "llmclipboard"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = []
|
||||
7
project/llmclipboard/uv.lock
generated
Normal file
7
project/llmclipboard/uv.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
version = 1
|
||||
requires-python = ">=3.10"
|
||||
|
||||
[[package]]
|
||||
name = "llmclipboard"
|
||||
version = "0.1.0"
|
||||
source = { virtual = "." }
|
||||
Loading…
Reference in New Issue
Block a user