copy_past_flow_new
Этот скрипт отслеживает буфер обмена и записывает скопированный текст в файл Excel, Word или CSV. Он использует такие библиотеки, как pyperclip, openpyxl и python-docx.user_2065311
python
a year ago
2.6 kB
4
Indexable
import pyperclip import time import logging from openpyxl import Workbook, load_workbook from docx import Document from os.path import exists class ClipboardMonitor: def __init__(self, file_path): self.file_path = file_path self.last_copied = "" def write_to_excel(self, text): if exists(self.file_path): workbook = load_workbook(self.file_path) else: workbook = Workbook() sheet = workbook.active sheet.append([text]) workbook.save(self.file_path) def write_to_word(self, text): if exists(self.file_path): document = Document(self.file_path) else: document = Document() document.add_paragraph(text) document.save(self.file_path) def write_to_csv(self, text): with open(self.file_path, 'a', encoding='utf-8') as f: f.write(text + "\n") def monitor_clipboard(self): while True: try: current_text = pyperclip.paste() if current_text != self.last_copied: self.last_copied = current_text if self.file_path.endswith('.xlsx'): self.write_to_excel(current_text) elif self.file_path.endswith('.docx'): self.write_to_word(current_text) elif self.file_path.endswith('.csv'): self.write_to_csv(current_text) else: self.write_to_csv(current_text) # Default to CSV if file type is unknown logging.info(f"Copied to file: {current_text}") except Exception as e: self.write_to_csv(f"Невозможно вставить данные: {str(e)}") logging.error(f"Error copying data: {str(e)}") time.sleep(2) # Увеличиваем интервал ожидания def main(): logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') file_path = input("Введите адрес файла для копирования данных (например, file.xlsx, file.docx, file.csv): ") clipboard_monitor = ClipboardMonitor(file_path) print("Наблюдение за буфером обмена начато. Для остановки нажмите Ctrl+C") try: clipboard_monitor.monitor_clipboard() except KeyboardInterrupt: print("Наблюдение за буфером обмена остановлено.") if __name__ == "__main__": main()
Editor is loading...
Leave a Comment