def loadFile(self): path = os.path.join('/docs/github/Opal/src/ui/view/opalview', 'bookInfo.html') out = StringIO() htmlhandler = rt.RichTextHTMLHandler() buffer = self.rtc.GetBuffer() # htmlhandler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) htmlhandler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) print 'canload:', htmlhandler.CanLoad() print 'cansave:', htmlhandler.CanSave() print 'CanHandle', htmlhandler.CanHandle('bookInfo.html') rt.RichTextBuffer.AddHandler(htmlhandler) buffer.AddHandler(htmlhandler) # buffer.LoadStream(stream, rt.RICHTEXT_TYPE_HTML) # out.write(self.book.bookDescription) # out.seek(0) # htmlhandler.LoadStream(buffer, out) # htmlhandler.LoadFile(path,'text') if self.book.bookDescription != None: self.rtc.AppendText(self.book.bookDescription) # htmlhandler.LoadStream(buffer, out.getvalue()) # self.rtc.Refresh() # buffer = self.rtc.GetBuffer() # you have to specify the type of data to load and the control # must already have an instance of the handler to parse it # c self.rtc.Refresh()
def XMLtoHTML(texteXML): """ Converti un texte au format RichText (XML) en HTML """ if texteXML is None: return out = cStringIO.StringIO() handler = rt.RichTextXMLHandler() buff = rt.RichTextBuffer() out.write(texteXML) out.seek(0) handler.LoadStream(buff, out) # Get an instance of the html file handler, use it to save the # document to a StringIO stream handler2 = rt.RichTextHTMLHandler() handler2.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) handler2.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) stream = cStringIO.StringIO() if not handler2.SaveStream(buff, stream): return soup = BeautifulSoup(stream.getvalue().decode('utf-8'), "html5lib") return soup.html.body.prettify()
def OnFileViewHTML(self, evt): # Get an instance of the html file handler, use it to save the # document to a StringIO stream, and then display the # resulting html text in a dialog with a HtmlWindow. handler = rt.RichTextHTMLHandler() handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) handler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) import cStringIO stream = cStringIO.StringIO() if not handler.SaveStream(self.rtc.GetBuffer(), stream): return import wx.html dlg = wx.Dialog(self, title="HTML", style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER) html = wx.html.HtmlWindow(dlg, size=(500, 400), style=wx.BORDER_SUNKEN) html.SetPage(stream.getvalue()) btn = wx.Button(dlg, wx.ID_CANCEL) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(html, 1, wx.ALL | wx.EXPAND, 5) sizer.Add(btn, 0, wx.ALL | wx.CENTER, 10) dlg.SetSizer(sizer) sizer.Fit(dlg) dlg.ShowModal() handler.DeleteTemporaryImages()
def Export_to_HTML(self, fname): try: # If an existing file is selected ... if (fname != ""): # Get an HTML Handler handler = richtext.RichTextHTMLHandler() # Set the handler's encoding handler.SetEncoding('UTF-8') # Use the HTML Handler to save the file. # Note that the HTML Handler takes a wxRichTextBuffer argument handler.SaveFile(self.reportText.GetBuffer(), fname) # We need to edit the HTML file ever so slightly so that it KNOWS that it's using UTF8. # Open the HTML file tmpFile = open(fname, "r") # Read the HTML file tmpFileText = tmpFile.read() # Close the HTML file tmpFile.close() # Determine the position of the HTML HEAD tag insertPoint = tmpFileText.lower().find('<head>') + 6 # Insert the HTML5 META tag specifying the UTF8 character set in the HEAD block tmpFileText = tmpFileText[:insertPoint] + '<META charset="UTF-8">' + tmpFileText[insertPoint:] # Now open the HTML file for writing tmpFile = open(fname, "w") # Replace its contents with the edited contents tmpFile.write(tmpFileText) # Close the HTML file tmpFile.close() except: print "HTML FILE SAVE failure in TextReport.py" print sys.exc_info()[0] print sys.exc_info()[1] print
def loadFile(self): # path = os.path.join('/docs/github/Opal/src/ui/view/opalview', 'bookInfo.html') out = StringIO() htmlhandler = rt.RichTextHTMLHandler() buffer = self.rtc.GetBuffer() # htmlhandler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) htmlhandler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) logger.debug('canload: %s', htmlhandler.CanLoad()) logger.debug('cansave: %s', htmlhandler.CanSave()) logger.debug('CanHandle: %s', htmlhandler.CanHandle('bookInfo.html')) rt.RichTextBuffer.AddHandler(htmlhandler) # buffer.AddHandler(htmlhandler) try: if self.book != None: out.write(self.book.bookDescription) out.seek(0) except Exception as e: logger.error(e) # htmlhandler.LoadStream(buffer, out) # htmlhandler.LoadFile(path,'text') if self.book != None and self.book.bookDescription != None: self.rtc.AppendText(self.book.bookDescription) # htmlhandler.LoadStream(buffer, out.getvalue()) self.rtc.Refresh()
def AddRTCHandlers(self): if text.RichTextBuffer.FindHandlerByType( text.RICHTEXT_TYPE_HTML) is not None: return text.RichTextBuffer.AddHandler( text.RichTextXMLHandler(name="Other XML", ext="ox", type=99)) text.RichTextBuffer.AddHandler(text.RichTextHTMLHandler()) wx.FileSystem.AddHandler(wx.MemoryFSHandler())
def GetHTML(self): # Get an instance of the html file handler, use it to save the # document to a StringIO stream handler = rt.RichTextHTMLHandler() handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) handler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) stream = cStringIO.StringIO() if not handler.SaveStream(self.rtc.GetBuffer(), stream): return return stream.getvalue()
def OnFileSave(self, evt): # self.loadFile() # self.GetParent().save() handler = rt.RichTextHTMLHandler() handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) handler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) import cStringIO stream = cStringIO.StringIO() if not handler.SaveStream(self.rtc.GetBuffer(), stream): return html_content = stream.getvalue() print html_content
def GetHTML_base64(self): # Récupération de la source HTML handler = rt.RichTextHTMLHandler() handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) handler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) import cStringIO stream = cStringIO.StringIO() if not handler.SaveStream(self.ctrl_editeur.GetBuffer(), stream): return False source = stream.getvalue() source = source.decode("utf-8") for balise in ("<html>", "</html>", "<head>", "</head>", "<body>", "</body>"): source = source.replace(balise, "") return source
def AddRTCHandlers(self): # make sure we haven't already added them. if rt.RichTextBuffer.FindHandlerByType(rt.RICHTEXT_TYPE_HTML) is not None: return # This would normally go in your app's OnInit method. I'm # not sure why these file handlers are not loaded by # default by the C++ richtext code, I guess it's so you # can change the name or extension if you wanted... rt.RichTextBuffer.AddHandler(rt.RichTextHTMLHandler()) rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler()) # ...like this rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler(name="Noetext", ext="ntx", type=99)) # This is needed for the view as HTML option since we tell it # to store the images in the memory file system. wx.FileSystem.AddHandler(wx.MemoryFSHandler())
def OnFileOpen(self, evt): # This gives us a string suitable for the file dialog based on # the file handlers that are loaded handler = rt.RichTextHTMLHandler() rt.RichTextBuffer.AddHandler(handler) wildcard, types = rt.RichTextBuffer.GetExtWildcard(save=False) dlg = wx.FileDialog(self, "Choose a filename", wildcard=wildcard, style=wx.OPEN) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() if path: fileType = types[dlg.GetFilterIndex()] self.rtc.LoadFile(path, fileType) dlg.Destroy()
def GetHtmlText(self, imagesIncluses=False): # Récupération de la source HTML handler = rt.RichTextHTMLHandler() if imagesIncluses == True: handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) else: handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) handler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) import cStringIO stream = cStringIO.StringIO() if self.ctrl_texte == None and self.nb.GetPageCount() > 0: self.ctrl_texte = self.nb.GetPage(self.nb.GetSelection()) if not handler.SaveStream(self.ctrl_texte.GetBuffer(), stream): return False source = stream.getvalue() ## source = source.replace("<head></head>", head) source = source.decode("utf-8") return source
def GetHTML(self, imagesIncluses=True): # Récupération de la source HTML handler = rt.RichTextHTMLHandler() if imagesIncluses == True : ## handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES) else: handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) handler.SetFontSizeMapping([7,9,11,12,14,22,100]) import cStringIO stream = cStringIO.StringIO() if not handler.SaveStream(self.ctrl_editeur.GetBuffer(), stream): return False source = stream.getvalue() ## source = source.replace("<head></head>", head) source = source.decode("utf-8") listeImages = handler.GetTemporaryImageLocations() return source, listeImages, handler
def OnFileViewHTML(self): # Get an instance of the html file handler, use it to save the # document to a StringIO stream, and then display the # resulting html text in a dialog with a HtmlWindow. handler = rt.RichTextHTMLHandler() handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) handler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) import cStringIO stream = cStringIO.StringIO() if not handler.SaveStream(self.ctrl_editeur.GetBuffer(), stream): return source = stream.getvalue() head = """ <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> """ source = source.replace("<head></head>", head) source = source.decode("utf-8") #print source import wx.html dlg = wx.Dialog(self, title="HTML", style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER) html = wx.html.HtmlWindow(dlg, size=(500, 400), style=wx.BORDER_SUNKEN) html.SetPage(source) btn = wx.Button(dlg, wx.ID_CANCEL) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(html, 1, wx.ALL | wx.EXPAND, 5) sizer.Add(btn, 0, wx.ALL | wx.CENTER, 10) dlg.SetSizer(sizer) sizer.Fit(dlg) dlg.ShowModal() handler.DeleteTemporaryImages()
def GetHTML(self, imagesIncluses=True, base64=False): # Récupération de la source HTML handler = rt.RichTextHTMLHandler() if imagesIncluses == True: if base64 == True: handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) else: handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES) else: handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) handler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) stream = six.BytesIO() if 'phoenix' in wx.PlatformInfo: if not handler.SaveFile(self.ctrl_editeur.GetBuffer(), stream): return False else: if not handler.SaveStream(self.ctrl_editeur.GetBuffer(), stream): return False source = stream.getvalue() source = source.decode("utf-8") listeImages = handler.GetTemporaryImageLocations() return source, listeImages, handler
def __init__(self, parent, id=-1, size=wx.DefaultSize): #, bgcolor=None): wx.Panel.__init__(self, parent, id, size=size) #self.Text = wx.TextCtrl ( self, -1, # style = wx.TE_MULTILINE | wx.TE_PROCESS_ENTER ) import wx.richtext as rt self.Text = rt.RichTextCtrl ( self, style = wx.VSCROLL|wx.HSCROLL\ |wx.TE_MULTILINE|wx.TE_PROCESS_ENTER \ |wx.WANTS_CHARS ) # VERY ESSENTIAL TO CATCH ARROW KEYS #|wx.NO_BORDER # make sure we haven't already added them. if not (rt.RichTextBuffer.FindHandlerByType(rt.RICHTEXT_TYPE_HTML)): # This would normally go in your app's OnInit method. I'm # not sure why these file handlers are not loaded by # default by the C++ richtext code, I guess it's so you # can change the name or extension if you wanted... rt.RichTextBuffer.AddHandler(rt.RichTextHTMLHandler()) rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler()) """ # ...like this rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler(name="Other XML", ext="ox", type=99)) """ # This is needed for the view as HTML option since we tell it # to store the images in the memory file system. wx.FileSystem.AddHandler(wx.MemoryFSHandler()) self.Text.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) self.sizer = wx.BoxSizer(wx.VERTICAL) self.sizer.Add(self.Text, 1, wx.EXPAND) self.SetSizer(self.sizer)
def GetHtmlText(self, imagesIncluses=False): # Récupération de la source HTML handler = rt.RichTextHTMLHandler() if imagesIncluses == True : handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) else: handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) handler.SetFontSizeMapping([7,9,11,12,14,22,100]) stream = six.BytesIO() if self.ctrl_texte == None and self.nb.GetPageCount()>0 : self.ctrl_texte = self.nb.GetPage(self.nb.GetSelection()) if 'phoenix' in wx.PlatformInfo: if not handler.SaveFile(self.ctrl_texte.GetBuffer(), stream): return False else : if not handler.SaveStream(self.ctrl_texte.GetBuffer(), stream): return False source = stream.getvalue() if six.PY2: source = source.decode("utf-8") return source
def GetHTML_base64(self): # Récupération de la source HTML handler = rt.RichTextHTMLHandler() handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) handler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) stream = six.BytesIO() if 'phoenix' in wx.PlatformInfo: if not handler.SaveFile(self.ctrl_editeur.GetBuffer(), stream): return False else: if not handler.SaveStream(self.ctrl_editeur.GetBuffer(), stream): return False source = stream.getvalue() if six.PY2: source = source.decode("utf-8") for balise in (u"<html>", u"</html>", u"<head>", u"</head>", u"<body>", u"</body>"): if six.PY3 and isinstance(balise, str): balise = balise.encode("utf-8") source = source.replace(balise, b"") else: source = source.replace(balise, "") return source