Reputation: 251
I want to copy rich text format to the clipboard in windows. Here's what I've got:
import win32clipboard
CF_RTF = win32clipboard.RegisterClipboardFormat("Rich Text Format")
rtf = r"{\rtf1\ansi\deff0 {\pard This is a test\par}}"
win32clipboard.OpenClipboard(0)
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(CF_RTF, rtf)
win32clipboard.CloseClipboard()
When I run this, and paste the output into word, it is not formatted correctly:
Upvotes: 3
Views: 4616
Reputation: 251
Convert it to a bytearray like this:
import win32clipboard
CF_RTF = win32clipboard.RegisterClipboardFormat("Rich Text Format")
rtf = bytearray("{\\rtf1\\ansi\\deff0 {\\pard This is a test\\par}}", 'utf8')
win32clipboard.OpenClipboard(0)
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(CF_RTF, rtf)
win32clipboard.CloseClipboard()
On Linux:
import sys
import subprocess
def copy_rtf(string):
if str(type(string)) == "<class 'str'>":
string = bytearray(string, 'utf8')
subprocess.Popen(['xclip', '-selection', 'clipboard', '-t', 'text/rtf'], stdin=subprocess.PIPE).communicate(string)
text = r"{\rtf1\ansi\deff0 {\b This} is some text\row}"
copy_rtf(text)
Upvotes: 7