Ron
Ron

Reputation: 513

parse HTML in python wxWidgets TextCtrl

Is it possible, or is there a library that will allow me to parse HTML code inside the wx.TextCtrl widget?

Upvotes: 2

Views: 706

Answers (2)

ravenspoint
ravenspoint

Reputation: 20596

wxTextCtrl will display the HTML with all the tags

<html><body>Hello, world!</body></html>");

To render the html, you need to use wxHtmlWindow

w = wxHtmlWindow(this)
w.SetPage("<html><body>Hello, world!</body></html>")

Upvotes: 0

chown
chown

Reputation: 52778

Sure, just use myTextCtrl.GetValue(), then parse the string with something like BeautifulSoup, xml.dom.minidom, HTMLParser, etc:

from BeautifulSoup import BeautifulSoup

# lets say this is the text inside the TextCtrl:
# '<html><head><title>Page title</title></head><body><p id="firstpara" align="center">This is paragraph <b>one</b>.<p id="secondpara" align="blah">This is paragraph <b>two</b>.</html>'
#

htmlStr = myTextCtrl.GetValue()

soup = BeautifulSoup(htmlStr)
soup.contents[0].name
# u'html'

soup.contents[0].contents[0].name
# u'head'

head = soup.contents[0].contents[0]
head.parent.name
# u'html'

head.next
# <title>Page title</title>

head.nextSibling.name
# u'body'

head.nextSibling.contents[0]
# <p id="firstpara" align="center">This is paragraph <b>one</b>.</p>

head.nextSibling.contents[0].nextSibling
# <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>

Upvotes: 1

Related Questions