
如果我從 Pidgin 即時通訊視窗中剪下一些 HTML,我可以輕鬆地將其逐字貼到 Thunderbird 中的新 HTML 電子郵件中。所有格式(字體、顏色等)都保留,因此看來我的 Ubuntu 13.10 桌面剪貼簿必須在某處有 HTML 來源。
但我想調整 HTML 原始碼。
當 HTML 原始碼位於剪貼簿中時,我如何才能真正獲得它?我想將其放入文字檔案中,在 Vim 或其他檔案中處理標記,然後在網頁中使用此 HTML 原始程式碼或將其提供給 Thunderbird 的「插入 → HTML」。
嗯,也許是這樣的貼上影像(在將剪貼簿上的圖形寫入磁碟?) ,但使用request_rich_text()
?request_image()
當我想從剪貼簿獲取 HTML 原始碼的時候,我不介意使用一個小的 Python 腳本。
剪貼簿中的內容實際上可能是「富文本」。
Python 腳本來自這個答案輸出
Current clipboard offers formats: ('TIMESTAMP', 'TARGETS', 'MULTIPLE',
'SAVE_TARGETS', 'COMPOUND_TEXT', 'STRING', 'TEXT', 'UTF8_STRING', 'text/html',
'text/plain')
原來我的 Pidgin 日誌是 HTML 格式的,所以這是一種取得方式這HTML 原始碼,完全繞過剪貼簿。我仍然對原始問題的答案感興趣(如何從剪貼簿檢索 HTML)。
答案1
答案2
我明白你想表達什麼。嘗試貼上到需要所見即所得的內容中,在那裡進行編輯,然後將貼上複製到 Thunderbird 中?
也許 bluegriffon 或 libreoffice writer 會起作用。
答案3
這是對您的腳本的修改,它實際上允許編輯直接html。
它還可以處理字元編碼問題:如果您回覆使用 Windows 的人發送的電子郵件,很可能編碼是 UTF-16,這不利於編輯。您可能需要安裝chardet
模組在Python中。
替換'vi'
為您選擇的文字編輯器,格式為subprocess.call(...
.
#!/usr/bin/env python
import gtk
import chardet
import os
import getopt
import subprocess
dtype = 'text/html'
htmlclip = gtk.Clipboard().wait_for_contents(dtype).data
encoding = chardet.detect(htmlclip)['encoding']
# Shove the clipboard to a temporary file
tmpfn = '/tmp/htmlclip_%i' % os.getpid()
with open (tmpfn, 'w') as editfile:
editfile.write(htmlclip.decode(encoding))
# Manually edit the temporary file
subprocess.call(['vi', tmpfn])
with open (tmpfn, 'r') as editfile:
htmlclip = editfile.read().encode(encoding)
# Put the modified data back to clipboard
gtk.Clipboard().set_with_data(
[(dtype,0,0)],
lambda cb, sd, info, data: sd.set(dtype, 8, htmlclip),
lambda cb, d: None )
gtk.Clipboard().set_can_store([(dtype,0,0)])
gtk.Clipboard().store()
這會執行完整的編輯週期,並「就地」修改剪貼簿。
我用它來彌補 Thunderbird 令人討厭的 html 編輯器功能的缺失:
- 全選在ctrl+a訊息撰寫視窗中
- ctrl+c
- 運行上面的腳本,這將打開一個編輯器 –
- 對 html 來源進行更改
- 儲存並退出
- ctrl+v撰寫視窗中的 會用您的 html 編輯版本覆蓋整個內容。
答案4
有一個答案直接使用該xclip
實用程式:
xclip -selection clipboard -o -t text/html
我發現它現在是最簡單、最可靠的,因為 GTK3/Python3 似乎引入了一些改變,打破了原來的答案。