简体   繁体   中英

How to export wxpython gui text in excel file?

I wrote code which takes text data from sql server and puts it in the wxpython static text. Now I want to know is there some menu event widgets which I can use to export my text in xlsx file? Or can you give me some hints how to make export event.

Yes, wxPython comes with menu events. The most common is wx.EVT_MENU. you can read about how to create menus at the following links:

Normally you would create a menu item labeled "Save" and bind it using wx.EVT_MENU to some method such as "onSave". Then in your onSave method, you would grab the contents of your StaticText widgets and save them to an Excel format. Then you could use xlwt to write the file out. Alternatively, you could use Python's csv module to write it out too as csv files can be opened by Excel.

If you absolutely, positively want to write out in Microsoft's format, then you'll probably want to take a look at XlsxWriter ( https://pypi.python.org/pypi/XlsxWriter )

Seems like all you'd need to do is to loop over all the wxTexts in the app and pull their text content. From there, you would be able to use xlrd (after you installed it), to open a workbook, choose a sheetname, and start writing the contents of the wxTexts to a file, under something like this:

widget_name | widget_content
-----------   --------------
title_text     "Hello world!"
middle_text    "Foo bar?"
bottom_text    "Spam..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM