有没有办法在python或vbscript或.bat中从剪贴板复制文本并以utf-8编码写入文件? [重复]

时间:2022-02-21 16:21:39

Possible Duplicate:
How do I read text from the (windows) clipboard from python?

可能重复:如何从python中读取(windows)剪贴板中的文本?

Is there a way in python or vb or .bat to copy text from clipboard and write to a file in utf-8 encoding?

有没有办法在python或vb或.bat中从剪贴板复制文本并以utf-8编码写入文件?

1 个解决方案

#1


0  

There is no direct access to the clipboard without third-party COM objects. You can use a workaround to get its contents.

没有第三方COM对象,无法直接访问剪贴板。您可以使用变通方法来获取其内容。

' Grab the contents of the clipboard
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
strClipboard = objIE.document.parentwindow.clipboardData.GetData("text")
objIE.Quit

You can read more about this in my article Scripting the Clipboard Contents in WSH.

您可以在我的文章Scripting WSH中的剪贴板内容中阅读更多相关内容。

Once you have the contents, you can employ the help of .Net to encode it.

获得内容后,您可以使用.Net的帮助对其进行编码。

Set Encoder = CreateObject("System.Text.UTF8Encoding") 
strClipboardB = Encoder.GetBytes_4(strClipboard) 'get bytes
Set Encoder = Nothing

I should warn you, I've never tested the clipboard method with UTF8 text. It should work, but I'm not sure off the top of my head how that class handles UTF8 characters.

我应该警告你,我从来没有用UTF8文本测试剪贴板方法。它应该可以工作,但我不确定该类如何处理UTF8字符。

#1


0  

There is no direct access to the clipboard without third-party COM objects. You can use a workaround to get its contents.

没有第三方COM对象,无法直接访问剪贴板。您可以使用变通方法来获取其内容。

' Grab the contents of the clipboard
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
strClipboard = objIE.document.parentwindow.clipboardData.GetData("text")
objIE.Quit

You can read more about this in my article Scripting the Clipboard Contents in WSH.

您可以在我的文章Scripting WSH中的剪贴板内容中阅读更多相关内容。

Once you have the contents, you can employ the help of .Net to encode it.

获得内容后,您可以使用.Net的帮助对其进行编码。

Set Encoder = CreateObject("System.Text.UTF8Encoding") 
strClipboardB = Encoder.GetBytes_4(strClipboard) 'get bytes
Set Encoder = Nothing

I should warn you, I've never tested the clipboard method with UTF8 text. It should work, but I'm not sure off the top of my head how that class handles UTF8 characters.

我应该警告你,我从来没有用UTF8文本测试剪贴板方法。它应该可以工作,但我不确定该类如何处理UTF8字符。