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

后端 未结 3 1174
醉酒成梦
醉酒成梦 2020-12-11 19:10

I\'m writting a Word document and I\'d like to paste a formatted code-snippets directly from clipboard.

At this moment I am able to write these snippets into an .htm

相关标签:
3条回答
  • 2020-12-11 19:23

    Nobody mentioned yet klembord. It works on Linux and Windows, and supports HTML clipboard format.

    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. 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.

    Example output

    When pasted to a rich text editor, the output from the example above would look like this:

    0 讨论(0)
  • 2020-12-11 19:27

    I used com interface to do it. http://en.wikipedia.org/wiki/Component_Object_Model

    The simpliest way: - create word template in microsoft word. - paste text from code in your template.

    0 讨论(0)
  • 2020-12-11 19:38

    Well, I've found a solution for this.

    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

    Download the pywin32 module and with win32clipboard you can do everything.

    0 讨论(0)
提交回复
热议问题