简体   繁体   English

如何以格式化文本从 html 文件复制到 Python 中的剪贴板?

[英]How can I copy from an html file to the clipboard in Python in formatted text?

I'm writting a Word document and I'd like to paste a formatted code-snippets directly from clipboard.我正在写一个 Word 文档,我想直接从剪贴板粘贴格式化的代码片段。

At this moment I am able to write these snippets into an .html file to the harddisk.此时,我可以将这些片段写入硬盘的 .html 文件中。 My goal is to extend my Python script and load this .html file on the clipboard in "formatted text" to directly paste at Word.我的目标是扩展我的 Python 脚本并在剪贴板上以“格式化文本”加载此 .html 文件以直接粘贴到 Word。

Does anyone knows any way to do this in Python?有谁知道在 Python 中执行此操作的任何方法?

Thanks in advance.提前致谢。

Sherab谢拉布

Well, I've found a solution for this.嗯,我已经找到了解决方案。

http://code.activestate.com/recipes/474121-getting-html-from-the-windows-clipboard/ http://code.activestate.com/recipes/474121-getting-html-from-the-windows-clipboard/

It's work pretty well... if anyone wants more information about the clipboard just take a look here:它工作得很好......如果有人想要更多关于剪贴板的信息,请看这里:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms649013(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/windows/desktop/ms649013(v=vs.85).aspx

Download the pywin32 module and with win32clipboard you can do everything.下载 pywin32 模块,使用 win32clipboard 你可以做任何事情。

Nobody mentioned yet klembord .还没有人提到克伦堡 It works on Linux and Windows, and supports HTML clipboard format .它适用于 Linux 和 Windows,并支持HTML 剪贴板格式

Installation安装

pip install klembord

Usage用法

import klembord
klembord.init()

# Case 1
klembord.set_with_rich_text('', 'Normal text, <i>Italic text</i>, <b>Bold text</b>, Normal text')

# Case 2
klembord.set_with_rich_text('', 'This is a <a href="https://github.com/OzymandiasTheGreat/klembord">link</a>')

Short explanation简短说明

The set_with_rich_text takes two arguments. set_with_rich_text有两个参数。 The first argument is the plain text alternative, which is used if you paste the content somewhere that does not support rich text formatting (such as Notepad).第一个参数是纯文本替代,如果您将内容粘贴到不支持富文本格式的位置(例如记事本),则使用它。 The second argument is the html formatted clipboard, which supports for example <a> , <i> and <b> HTML tags.第二个参数是 html 格式的剪贴板,它支持例如<a><i><b> HTML 标签。

Example output示例输出

When pasted to a rich text editor, the output from the example above would look like this:当粘贴到富文本编辑器时,上面示例的输出将如下所示:

在此处输入图片说明

I used com interface to do it.我使用com接口来做到这一点。 http://en.wikipedia.org/wiki/Component_Object_Model http://en.wikipedia.org/wiki/Component_Object_Model

The simpliest way: - create word template in microsoft word.最简单的方法: - 在microsoft word 中创建word 模板。 - paste text from code in your template. - 将代码中的文本粘贴到模板中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM