[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

if name == 'main':


to test me: html2text.py media\html2text-test\htmlmail1.html


parse file name in commandline, display result in tkinter Text


file assumed to be in Unicode platform default, but text need not be


import sys, tkinter
text = open(sys.argv[1], 'r').read()
text = html2text(text)
t = tkinter.Text()
t.insert('1.0', text)
t.pack()
t.mainloop()


After this example and chapter had been written and finalized, I did a
search for HTML-to-text translators on the Web to try to find better
options, and I discovered a Python-coded solution which is much more
complete and robust than the simple prototype script here. Regrettably,
I also discovered that this system is named the same as the script
listed here!
This was unintentional and unforeseen (alas, developers are predis-
posed to think alike). For details on this more widely tested and much
better alternative, search the Web for html2text. It’s open source, but
follows the GPL license, and is available only for Python 2.X at this
writing (e.g., it uses the 2.X sgmllib which has been removed in favor
of the new html.parser in 3.X). Unfortunately, its GPL license may raise
copyright concerns if shipped with PyMailGUI in this book’s example
package or otherwise; worse, its 2.X status means it cannot be used at
all with this book’s 3.X examples today.
There are additional plain-text extractor options on the Web worth
checking out, including BeautifulSoup and yet another named
html2text.py (no, really!). They also appear to be available for just 2.X
today, though naturally, this story may change by the time you read this
note. There’s no reason to reinvent the wheel, unless existing wheels
don’t fit your cart!

mailconfig: User Configurations


In Example 14-9, PyMailGUI’s mailconfig user settings module is listed. This program
has its own version of this module because many of its settings are unique for PyMail-
GUI. To use the program for reading your own email, set its initial variables to reflect
your POP and SMTP server names and login parameters. The variables in this module
also allow the user to tailor the appearance and operation of the program without
finding and editing actual program logic.


As is, this is a single-account configuration. We could generalize this module’s code to
allow for multiple email accounts, selected by input at the console when first imported;


PyMailGUI Implementation| 1105
Free download pdf