Why convert PY to PDF?
A PDF of Python source code can be opened on any device without a Python installation and is used for code review printouts, documentation, and assignment submission. PDF preserves syntax highlighting and pagination in a fixed layout.
How to convert PY to PDF
VS Code FREE
Open the .py file in VS Code, install the vscode-print or PrintCode extension, use the Print command, then set the browser print destination to Save as PDF.
Pygments (pygmentize) OPEN-SOURCE
Install with pip install pygments (requires a LaTeX distribution for PDF output), then run pygmentize -f pdf -o output.pdf yourfile.py to produce a syntax-highlighted PDF.
Notepad++ FREE
Open the .py file in Notepad++, go to File > Print, and select Microsoft Print to PDF as the printer to save a plain unstyled PDF without syntax highlighting.
About these formats
A .py file is a Python source code file - plain text containing a program or script written in the Python language. To read the code, open it in any text editor. To run it, install Python…
Open .PY details →A PDF (Portable Document Format) is a fixed-layout document that looks identical on every device - fonts, images, and layout are all embedded. Open it in any browser or the built-in viewer…
Open .PDF details →Quality & what to watch
- The resulting PDF is a visual snapshot of the code - it cannot be executed and contains no Python runtime data.
- Pygments requires a LaTeX distribution (TeX Live or MiKTeX) to produce PDF output; if LaTeX is not installed, use
-f htmlto generate an HTML file and print that to PDF from a browser. - On macOS, any application's print dialog has a Save as PDF button in the lower left - no extra software is needed for a basic unstyled export.
Frequently asked questions
Will the PDF include syntax highlighting?
Do I need LaTeX to use Pygments for PDF output?
pygmentize -f pdf generates a LaTeX file and compiles it. Without LaTeX, use -f html to produce an HTML file and print that to PDF from a browser.