1

I'm looking to automate the formatting of sources in a Microsoft word document(.docx). The problem is that some of the text in the new format has to be in italics. Is there a way in python to format text in italics to the clipboard? If you manually(ctrl + c) copy italics, the italic part of the string is still kept in the clipboard. Because when you paste it out(ctrl + v) it's still in italics. This is why I\m wondering if it\s possible in python.

I've already looked at pyperclip, but they only provide information on how to copy plain strings. (https://pyperclip.readthedocs.io/en/latest/introduction.html).

Mr. C
  • 13
  • 2
  • `"If you manually(ctrl + c) copy italics, the italic part of the string is still kept in the clipboard. Because when you paste it out(ctrl + v) it's still in italics."` I can see the line of thought here, but i'm afraid this assumption is not quite correct. Try copy pasting a sentence with italics from a `.docx` to somewhere else, such as notepad. So, to you, i have a suggestion. Perhaps pursue exploring how to get italics into certain words while writing them via python, (if you even want to be using python in the first place.) Essentially, explore how to add formatting while writing – Paritosh Singh Jul 23 '19 at 14:20
  • 1
    Thank you, Ill change my language to nodejs and use https://www.npmjs.com/package/clipboardy and https://www.npmjs.com/package/node-key-sender. Since it's a set format I can activate italics throuch ctr + k in my language(cursive). – Mr. C Jul 23 '19 at 15:51
  • As far as I know, MS Word rich text will be put in the clipboard as either RTF or HTML, or both. Just find a way to view _all_ formats added to the clipboard, and analyse the contents to see what's actually in there, how it's formatted, and how you can manipulate it. – Nyerguds Aug 01 '19 at 09:03

1 Answers1

0

It is possible with klembord.

Installation

pip install klembord

Code to set italic text to clipboard

import klembord
klembord.init()

# Set HTML formatted clipboard
klembord.set_with_rich_text('', 'Normal text, <i>Italic text</i>')

Short explanation

The set_with_rich_text() takes two arguments. The first argument is what is set to the "plain text" clipboard, which is then used if user pastes for example to Notepad. The second argument is the "HTML formatted clipboard", which is uses if user pastes to a rich text editor, such as Word.

Output when pasted to Word

example output

Niko Föhr
  • 28,336
  • 10
  • 93
  • 96