Get plain text from QLabel using Rich Text

I have a QLabel that contains rich text.
I want to extract only the actual (visible) "text" from QLabel and not one of the code to format.
I need a function similar to the '.toPlainText' method of another Qt Widgets .

I can’t just call .text() , and the string will process the html tags as suggested in this thread Get plain text from a QString with HTML tags , because the returned QString contains all the crap <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> .

How to extract plain text?

(I am open to any method, even if indirect, for example, pre-existing functions that convert html to plain text)

Thanks!

Specifications:
python 2.7.2
PyQt4
Windows 7

+6
source share
2 answers

Use QTextDocument to perform the conversion:

 doc = QtGui.QTextDocument() doc.setHtml(label.text()) text = doc.toPlainText() 
+10
source

Here's a messy job (for python - PyQt)

 def Extract_PlainText(label): Rtf_text = label.text() Temp_Obj = QtGui.QTextEdit() Temp_Obj.setText(Rtf_text) Plain_text = Temp_Obj.toPlainText() del Temp_Obj return Plain_text 

Inspired by http://bytes.com/topic/net/answers/707370-convert-rtf-plain-text

+1
source

Source: https://habr.com/ru/post/906177/


All Articles