wxPython RichTextCtrlから画像を取り出す方法
xmlファイルに吐き出して中の画像を取り出しました。
とりあえず私の環境ではpngだった。
imagetype以外は別の処理が必要そうだ。
Imageファイルのヘッダー文字から画像ファイルの種類判定しても良いかもしれない。
# -*- coding: utf-8 -*- ########################################################################### ## Python code generated with wxFormBuilder (version Oct 26 2018) ## http://www.wxformbuilder.org/ ## ## PLEASE DO *NOT* EDIT THIS FILE! ########################################################################### import wx import wx.xrc import wx.richtext # for conv_xml2file import os import codecs import binascii import hashlib import xml.etree.ElementTree as ET try: from StringIO import StringIO except ImportError: from io import StringIO ########################################################################### ## conv_xml2py ########################################################################### def conv_xml2py(xmlpath): base_dir ,ext = os.path.splitext(xmlpath) if not os.path.exists(base_dir): os.mkdir(base_dir) tree = ET.ElementTree(file=xmlpath) md5dat = hashlib.md5() line = 0 img_num = 0 data = '' for item in tree.iter(): item_type = item.tag.split('}')[1] text = item.text if text is None : text = '' text = text.strip() if item_type == 'paragraph' : line += 1 if line != 1: data += '\n' elif item_type == 'text' : data += text elif item_type == 'data' : img_num += 1 img_bin = binascii.unhexlify(text) md5dat.update(img_bin) filename = 'img-%06d-%s.png'%(img_num,md5dat.digest().hex()) img_num += 1 filepath = os.path.join(base_dir,filename) fp = open(filepath,'wb+') fp.write( img_bin ) fp.close() data += '"%s"'%(filename) filepath_py = os.path.join(base_dir,'aaaa.txt') fp_py = codecs.open(filepath_py,'w+','utf8') fp_py.write(data) fp_py.close() ########################################################################### ## Class MyFrame1 ########################################################################### class MyFrame1 ( wx.Frame ): def __init__( self, parent ): wx.Frame.__init__ ( \ self, parent, id = wx.ID_ANY, title = wx.EmptyString, \ pos = wx.DefaultPosition, size = wx.Size( 500,300 ), \ style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL ) self.SetSizeHints( wx.DefaultSize, wx.DefaultSize ) bSizer6 = wx.BoxSizer( wx.VERTICAL ) self.m_button1 = wx.Button( \ self, wx.ID_ANY, u"MyButton", \ wx.DefaultPosition, wx.DefaultSize, 0 ) bSizer6.Add( self.m_button1, 0, wx.ALL, 5 ) self.m_richText1 = wx.richtext.RichTextCtrl( \ self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, \ wx.DefaultSize, \ 0|wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER|wx.WANTS_CHARS ) bSizer6.Add( self.m_richText1, 1, wx.EXPAND |wx.ALL, 5 ) self.SetSizer( bSizer6 ) self.Layout() self.Centre( wx.BOTH ) # Connect Events self.m_button1.Bind( wx.EVT_LEFT_DOWN, self.Click_MyButton ) def __del__( self ): pass # Virtual event handlers, overide them in your derived class def Click_MyButton( self, event ): openFileDialog = wx.FileDialog(None, "Open a file", "", "", "*.xml", wx.FD_SAVE) if openFileDialog.ShowModal() == wx.ID_CANCEL: return project_path = path = openFileDialog.GetPath() handler = wx.richtext.RichTextXMLHandler() rt_buffer = self.m_richText1.GetBuffer() handler.SaveFile(rt_buffer,project_path) conv_xml2py(project_path) app = wx.App(False) #create an object of CalcFrame frame = MyFrame1(None) #show the frame frame.Show(True) #start the applications app.MainLoop()