I’m trying to create a small program that can copy and paste rich text (bold, italics, etc…) from the clipboard to my application.
(OS: Windows 11) (Python version: 3.12)
I have installed the pyclip package (current version 0.7.0) which, according to the package documentation, supports both text and binary data from the clipboard.
I have written this test code following the instructions in the pyclip documentation:
import pyclip
texto = pyclip.paste()
print(texto)
pyclip.clear()
When I copy the following text from my email:
This is bold text in my email.
The screen result of the script is correct:
This is xc2xa0bold textxc2xa0 in my email.
However, if I copy similar text from a Microsoft Word document:
“This is bold text in a Word document.”
The result on the screen is not correct, the encoding does not appear:
b’This is bold text in a Word document.’
I understand that this is because Word does not use bold with ‘utf-8’ encoding. To retrieve the bold text information from the clipboard, you might need to directly access the Windows API. However, I am not familiar with the specific details of how to do this.
I have also tried with ctypes and win32clipboard with the same result following the advice in the thread text
Thank you in advance!
Gaspar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.