How to physically print python code in color from

2020-03-01 20:53发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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.

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.



回答4:

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.



回答5:

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.