[Python 3.6][wx 4.0.0][RichtextCtrl] Export and import text in xml format

Hello,

I’m
migrating an application that was originally developed in Python 2 and wxPython 3.0.2 to Python 3 and wxPython 4.0.0. The last thing I get stuck with is the use of the XML format in RichTextCtrl to keep the layout

For backup, in python 2, I wrote this (where rtc is a RichTextCtrl and txt a string then saved in a database) :

import cStringIO as io
handler = wx.richtext.RichTextXMLHandler()
stream = io.StringIO()
handler.SaveStream(rtc.GetBuffer(), stream)
stream.seek(0)
txt = stream.read()

For reading :

stream = io.StringIO()
handler = wx.richtext.RichTextXMLHandler()
buffer = rtc.GetBuffer()
buffer.AddHandler(handler)
stream.write(txt)
stream.seek(0)
handler.LoadStream(buffer, stream)

With wxPython 4.0.0, this way does not work as RichTextXMLHandler has no longer methods "LoadStream" and "SaveStream". There are now methods "ImportXML" and "ExportXML" but I have not found any examples to understand how to use them.

Thank you in advance for your answer

Try LoadFile(buffer, stream) instead of LoadStream().

Scott

···

On Thu, 27 Apr 2017, Guillaume DURIAUD wrote:

I'm migrating an application that was originally developed in Python 2 and
wxPython 3.0.2 to Python 3 and wxPython 4.0.0. The last thing I get stuck
with is the use of the XML format in RichTextCtrl to keep the layout

For backup, in python 2, I wrote this (where rtc is a RichTextCtrl and txt
a string then saved in a database) :

import cStringIO as io handler = wx.richtext.RichTextXMLHandler() stream = io.StringIO() handler.SaveStream(rtc.GetBuffer(), stream) stream.seek(0) txt = stream.read()
For reading :
stream = io.StringIO() handler = wx.richtext.RichTextXMLHandler() buffer = rtc.GetBuffer() buffer.AddHandler(handler) stream.write(txt) stream.seek(0) handler.LoadStream(buffer, stream)
With wxPython 4.0.0, this way does not work as RichTextXMLHandler has no lo
nger methods "LoadStream" and "SaveStream". There are now methods "ImportXM
L" and "ExportXML" but I have not found any examples to understand how to u
se them.
Thank you in advance for your answer

I use it like that:

import io

import wx.richtext as rt

if rt.RichTextBuffer.FindHandlerByType(rt.RICHTEXT_TYPE_XML) is None:

rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler())

rtc = rt.RichTextCtrl(…)

def GetXML():

buffer = rtc.GetBuffer()

xmlhandler = buffer.FindHandlerByType(rt.RICHTEXT_TYPE_XML)

with io.BytesIO() as stream:

xmlhandler.SaveFile(buffer, stream)

xml = stream.getvalue()

return xml

def PutXML(xml):

buffer = rtc.GetBuffer()

xmlhandler = buffer.FindHandlerByType(rt.RICHTEXT_TYPE_XML)

with io.BytesIO(xml) as stream:

xmlhandler.LoadFile(buffer, stream)

``

I think the io.BytesIO() is better on Python 3.

woss

  1. április 27., csütörtök 17:04:52 UTC+2 időpontban Guillaume DURIAUD a következőt írta:
···

Hello,

I’m
migrating an application that was originally developed in Python 2 and wxPython 3.0.2 to Python 3 and wxPython 4.0.0. The last thing I get stuck with is the use of the XML format in RichTextCtrl to keep the layout

For backup, in python 2, I wrote this (where rtc is a RichTextCtrl and txt a string then saved in a database) :

import cStringIO as io
handler = wx.richtext.RichTextXMLHandler()
stream = io.StringIO()
handler.SaveStream(rtc.GetBuffer(), stream)
stream.seek(0)
txt = stream.read()

For reading :

stream = io.StringIO()
handler = wx.richtext.RichTextXMLHandler()
buffer = rtc.GetBuffer()
buffer.AddHandler(handler)
stream.write(txt)
stream.seek(0)
handler.LoadStream(buffer, stream)

With wxPython 4.0.0, this way does not work as RichTextXMLHandler has no longer methods “LoadStream” and “SaveStream”. There are now methods “ImportXML” and “ExportXML” but I have not found any examples to understand how to use them.

Thank you in advance for your answer

Thanks tou both, It works !

My datas are saved in database as a string. I do that :

stream = io.BytesIO()

stream.write(bytes(txt, encoding = “utf8”))
stream.seek(0)

handler = wx.richtext.RichTextXMLHandler()
buffer = rtc.GetBuffer()
buffer.AddHandler(handler)

handler.LoadFile(buffer, stream)