[Idle-dev] [ idlefork-Bugs-837085 ] Print colored

SourceForge.net noreply at sourceforge.net
Sat Nov 8 18:20:21 EST 2003

Bugs item #837085, was opened at 2003-11-06 01:47
Message generated for change (Comment added) made by nobody
You can respond by visiting: 

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Heiko Selber (drhok)
Assigned to: Nobody/Anonymous (nobody)
Summary: Print colored

Initial Comment:
It would be nice if the syntax highlighting could be 

Currently, the print function is in black and white only, 
even on color printers.


Comment By: Nobody/Anonymous (nobody)
Date: 2003-11-08 15:20

Logged In: NO 

I wanted to print in color too. As far as I can tell, IDLE sends the contents of your editor window to 
notepad.exe (on windows) for printing; I don't know what it does on other platforms. Notepad doesn't do 
color but I found another way.

I was reading the "Python Cookbook" and there's a recipe in there for formatting python source in colorized 

Jürgen Hermann has made a "colorizer" for the MoinMoin project
(MoinMoin urls:

You can find the colorizer here:

If you want to get it to give you output the same as IDLE a few small modifications are needed:

In python, all name tokens are treated the same, 'if' is a name just like any identifier, so all "names" are 
colored the same. To get output like IDLE's, you want only identifiers occuring after "class" and "def" to be 

1. We need a new token type, so add the line

_DEForCLASS = token.NT_OFFSET + 3


_KEYWORD       = token.NT_OFFSET + 1
_TEXT          = token.NT_OFFSET + 2

(you can use any identifier you want, it doesn't have to be 

2. Next, edit the '_colors' dictionary to get more IDLE-like colors and add an entry for _DEForCLASS

_colors = {
    token.NUMBER:       '#000000', #black
    token.OP:              '#000000', #black
    token.STRING:        '#008000', #green
    tokenize.COMMENT: '#ff0000', #red
    token.NAME:           '#000000', #black
    token.ERRORTOKEN: '#FF8080',
    _KEYWORD:            '#ff8c00', #dark orange
    _TEXT:                  '#000000', #black
    _DEForCLASS:         '#0000ff', #blue

3. We need something to keep track of the previously scanned token for each current token so we can 
know to color a token blue if it follows a 'def' or 'class' token, so add the following to the __init__ function 
of the Parser class

        self.prev_tok = ''

4. Then in the if...elif statement with the comment "# map token type to a color group" in the __call__ 
function, add a branch to check if the previous token was "def" or "class"

        # map token type to a color group
        if token.LPAR <= toktype and toktype <= token.OP:
            toktype = token.OP
        elif toktype == token.NAME and keyword.iskeyword(toktext):
            toktype = _KEYWORD

#add this branch...
        elif toktype == token.NAME and self.prev_tok in ['def', 'class']:
            toktype = _NEWDEForCLASS

#in Python 2.3 you could just say self.prev_tok in 'def class'
#it might be slightly faster, but in practice it probably won't matter

5. Last but not least, somewhere before the end of the __call__ function put the following line, to remember 
the previous token for the elif from step 4.

        self.prev_tok = toktext

There ya go! This works great and is pretty simple to use. It's not as good as just clicking a menu option 
from within IDLE, but it will let you print your python source in color. Yay!

Many thanks to Jürgen Hermann for writing and making known this useful script. I hope this helps you out.

~Simon Forman
calroc at mindspring.com

BTW, if you want the background to be white (in the browser, I don't think it affects printing) make the 
following changes in the format function

 <! sys.stdout.write('<body bgColor="#ffffff"><pre><font face="Lucida,Courier New">')
 !> sys.stdout.write('<pre><font face="Lucida,Courier New">')


 <! sys.stdout.write('</font></pre></body>')
 !> sys.stdout.write('</font></pre>')


You can respond by visiting: 

More information about the IDLE-dev mailing list