Looks like this works if the number of selected rows are equal to the the number of selections from the editor. Tested with LibreOffice Calc.
import ctypes def custom_paste(): if ctypes.windll.user32.OpenClipboard(None): try: clipboard_handle = ctypes.windll.user32.GetClipboardData(1) if clipboard_handle: pointer_to_content = ctypes.windll.kernel32.GlobalLock(clipboard_handle) clipboard_data = ctypes.c_char_p(pointer_to_content).value ctypes.windll.kernel32.GlobalUnlock(clipboard_handle) else: raise ValueError('Invalid clipboard handle') except Exception as e: print('Error', e.message) return finally: # ensures that clipboard will be closed even in case of an exception ctypes.windll.user32.CloseClipboard() # there was no exception - start replacing targets lines = clipboard_data.splitlines() if len(lines) == editor.getSelections(): for i, line in enumerate(lines): editor.setTarget(editor.getSelectionNStart(i), editor.getSelectionNEnd(i)) editor.replaceTarget(line) else: print('Unable to open the clipboard - try again later') custom_paste()