How to physically print python code in color from IDLE?
Asked Answered
W

6

11

I have searched for an answer to this but the related solutions seem to concern 'print'ing in the interpreter.

I am wondering if it is possible to print (physically on paper) python code in color from IDLE?

I have gone to: File > Print Window in IDLE and it seems to just print out a black and white version without prompting whether to print in color etc.

Edit:

It seems like this might not be available so the option is to copy code to a text editor like SciTE and print from there - quite like the default IDLE syntax highlighting though.

Westfalen answered 30/5, 2013 at 10:5 Comment(0)
B
6

IDLE can't do it, but you can do it in an indirect way: Use LaTeX to format your script with the IDLE colors.

The listings package supports IDLE-like colorizing for python, but it's pretty well hidden: You must know to include a "preference file" that is mentioned in the listings manual, but without a description of its features. The following almost-minimal example will turn your script myscript.py into color PDF in the IDLE colors.

\documentclass{article}

\usepackage{listings}
\input{listings-python.prf}  % defines the python-idle-code style
\usepackage{textcomp} % Needed for straight quotes                               

\lstset{
 basicstyle=\normalsize\ttfamily, % size of the fonts for the code               
 language={Python},style={python-idle-code},
 showstringspaces=false,
 tabsize=4,
 upquote=true,   % Requires textcomp                                             
}

\oddsidemargin=-0.5cm
\textwidth=7in  % This just fits 80 characters at 10pt                           

\begin{document}
\lstinputlisting{myscript.py}
\end{document}

You could also include code snippets in a real LaTeX document.

Availability: You'll need at least listings version 1.5b (2013/08/26). Users of Texlive 2013 must download it from CTAN (e.g. with the command line texlive manager, tlmgr). It is part of TexLive 2016.

Unfortunately there still seems to be a problem with the CTAN version: The preferences file listings-python.prf is in the documentation folder for the package, not in tex/latex/listings where TeX can find it. You'll need to move it manually for the inclusion to work.

Biestings answered 28/11, 2013 at 22:54 Comment(5)
IDLE can do it using extensions (Python modules that can be run from the editor itself e.g. IDLE2HTML) which are given data about the editor window when run.Arvy
In TexLive 2016 (current version as of now), the file listings-python.prf is well located (/texmf-dist/tex/latex/listings/listings-python.prf) and LaTeX can find it without copying it.Mcneill
@ alexis This sounds promising but I feel more instruction is required please. When I download the listings package I get 8 files but where do I put them? In a particular folder in the IDLE installation perhaps? Then, can I literally cut and paste your script into the IDLE console and it will work? (Except I realize I will have to insert the complete path location and file name where you have "myscript.py".)Norbertonorbie
@waterman, that's not how it works. My answer shows how to format your code with LaTeX, a document formatting system. IDLE plays no part in that. The listings package comes standard with the texlive distribution.Biestings
PS the color scheme in listings almost exactly matches the IDLE color scheme.Biestings
C
3

Use the IDLE extension called IDLE2HTML.py (search for this). This lets IDLE print to an HTML file that has color in its style sheet. Then save the HTML file to a PDF (the color will still be there).

Corollary answered 26/10, 2014 at 11:52 Comment(0)
A
2

A good and simple solution is to use IDLE2HTML, an extension for IDLE.
The tool allows IDLE to save code from the editor window (or interactive shell) to a HTML file,
with colour included in the CSS format. After this, the file can be printed (as asked for) or its formatted code used with other programs such as word processors.
A reliable website currently to download the Python files and view some instructions is the Python Package Index (PyPI) - the page is https://pypi.python.org/pypi/IDLE2HTML or the pip module, built-in to newer Python versions, can install the files for you.

An useful advantage of this method is that it gets the colour formatting from IDLE instead of taking time analysing the code and using stored colours (which many other solutions do). This means it uses the actual colour scheme and works with different IDLE themes such as "IDLE Dark". This should result in less resources and shorter code being used.
Also, the current extension (version 2.0) could probably be adapted to use a different file format when saving the data because it has quite simple code.

Some details about how it works:

Since the tool is Python code running as an extension, it has access to a special variable provided by IDLE, which contains data about the editor window. The extend.txt file of the idlelib Python extension (that runs the editor) describes this variable called editwin:

An IDLE extension class is instantiated with a single argument, `editwin', an EditorWindow instance.

It also includes some other files about extending IDLE such as "config-main.def" and "config-extensions.def". The official Python documentation has more information.

Installation instructions:

Copy IDLE2HTML.py to the "idlelib" folder of your python installation.
Append the contents of "config-extensions.txt" to your "config-extensions.def" file (also in the idlelib folder) and restart IDLE.

How to use:

Select "Save as HTML" from the "Options" menu and specify the filename in the FileDialog window. The HTML file is created immediately.

A code tweak to work in Python 3.x:
Currently, the IDLE2HTML extension (version 2.0) won't work with Python 3.x until a small change is made to the code which I found in a fixed version included with the IDLEX Python module. Here is a file comparison, with the original on the left and the fixed IDLEX version on the right of the screenshot image.

File comparison image described above.

Description: if Python version is Python 3.x, use import tkinter as Tkinter instead of import Tkinter and import tkinter.filedialog as tkFileDialog instead of import tkFileDialog. The code shown uses sys.version from the built-in "sys" module in an if/else block to run the right code.

Arvy answered 18/6, 2016 at 14:23 Comment(2)
you mention in the "Installation Instructions" you have to "restart IDLE". What does this mean and how to you do it?Norbertonorbie
It means to exit IDLE if it is running and start it however to normally start it. The point is that IDLE loads extensions when it starts, so adding an extension has no effect until the next time IDLE is run. One must put the extension in the idlelib directory for each version of Python installed.Rhythmandblues
C
2

The easiest way is to open the '.py' file with Notepad++ and there you can select and modify your settings and preferences (any theme you want for your python codes) to export the python file to a colorful PDF file (using PDF Virtual Printer) or directly print it :-) .

OR

If you are using VS code, then simply install (PrintCode) extension and after installing:

  1. open VS Code and go to View
  2. Click on Command Palette
  3. search for " PrintCode "

It will export the python file as an HTML with your default internet browser and there you can directly print it or with a virtual printer, print your python document to a colorful PDF.

Clower answered 20/4, 2019 at 8:51 Comment(0)
T
0

I had the same problem and opened the py file into Visual Studio Code, then simply copied (Ctrl A / Ctrl C) and pasted the text (Ctrl V) into a Microsoft editor such as Word or RTF, or even outlook. The colors stay, including when printing. Note that in order to avoid the night mode set as a default in VS Code, go to File->Preferences->Color Theme and choose a day mode which is more suitable for printing.

Twofold answered 19/9, 2019 at 11:36 Comment(0)
K
0

I do this all the time with large amounts of code. Just install SciTE "https://www.scintilla.org/SciTEDownload.html" Copy and paste your code and print. I like to grab some tape and markers stick all the sheets on a wall and put some music on. Happy coding!

Kamerman answered 25/10, 2020 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.