Syntax highlighting
Keywords, strings, comments, decorators, numbers, function names and class names are colored like in an editor, or set in bold and italic for black-and-white printing.
Convert Python .py files to PDF with syntax highlighting and line numbers, directly in your browser with no upload.
or drag and drop .py, .pyw, .pyi, .pyx or .txt files here
Keywords, strings, comments, decorators, numbers, function names and class names are colored like in an editor, or set in bold and italic for black-and-white printing.
Every source line is numbered, tabs are expanded to real tab stops, and long lines wrap with a marker instead of running off the page.
Convert up to 100 files at once into separate PDFs in a ZIP, or into one PDF with a bookmark for each file and a contents page.
Your .py files are only read, never changed and never run.
Choose or drop .py files, paste code, or load the sample script. Up to 100 files, 10 MB each.
Select a file to see it highlighted with line numbers. Reorder files with the arrows.
Set paper size, font size, theme, line numbers, long-line handling, header and footer.
Preview the finished PDF, then download it, or download a ZIP when you convert several files.
Each option changes how the Python code is laid out on the page.
No. Your browser reads each file, highlights it and writes the PDF locally. The code is never sent to this website or any other server, and it is never executed. The only thing the page downloads is its own font files, which are embedded in the PDF. Nothing is stored after you close the tab.
Yes. The PDF contains real text set in an embedded monospace font, not a screenshot of your code. You can select and copy lines, search with Ctrl+F or Cmd+F, and zoom in without blur. Plagiarism checkers and screen readers can read it too.
A .py file is plain text, which is ideal for running and editing but awkward to hand to someone who only wants to read it. A PDF looks the same on every device, cannot be changed by accident, and prints predictably. These are the situations where people convert Python to PDF most often:
There is no single official way to print Python code to PDF. Editors, command-line tools and word processors can all do it, with different amounts of setup and different results.
| Method | Setup | Colors | Line numbers | Good to know |
|---|---|---|---|---|
| This PY to PDF converter | None, runs in the browser | Yes, three themes | Yes | Wraps long lines, batch and combined output, selectable text. Chinese, Japanese, Korean and emoji characters are replaced with a box. |
| VS Code | An extension | Depends on the extension | Depends on the extension | VS Code has no built-in print command, so you install an extension that opens the file in a browser and print from there. |
| PyCharm | PyCharm installed | Yes | Yes | File > Print, then pick your system's Save as PDF or Microsoft Print to PDF printer. One file at a time unless you print a whole directory. |
| IDLE | Comes with Python | No | No | File > Print Window sends plain text to the default printer, so you need a PDF printer set as default. |
| enscript or a2ps | Linux or macOS command line | Yes | Yes | Produces PostScript that you convert with ps2pdf. Fast for many files, but UTF-8 text beyond Latin-1 is poorly supported. |
| Pygments script | Python plus two packages | Yes, many styles | Yes | Flexible and scriptable. Pygments writes HTML or LaTeX, so a second tool turns that into a PDF. |
| Pasting into Word | Word or Google Docs | Only if the editor copies them | No | Autocorrect can change quotes and capitals, proportional fonts break alignment, and indentation is easy to damage. |
If you already work in PyCharm, printing from it is fine for one file. The online converter is the shortest route when you are on a school computer, a Chromebook or a tablet, when you have a folder of files, or when you need a result that does not depend on which editor and printer driver you have. Pasting code into a word processor is the method to avoid: if you must deliver a .docx, keep the code in a monospace font, switch off autocorrect, and join the parts with Merge Word Files afterwards.
.py files in the converter at the top of this page, or select Paste code.Code is set in a monospace font, so the number of characters that fit on a line is fixed by three things: the paper width, the margins and the font size. With the defaults (A4, portrait, normal margins, 9 point, line numbers on) a line holds 84 characters. That is more than the 79 characters that PEP 8 recommends as the maximum line length, so code that follows the style guide fits without wrapping.
Truncate is useful for a quick overview where one row per line matters more than the full text, for example a listing of a long data file. For anything you hand in, keep Wrap.
A project is usually several modules. Add them all, order them the way a reader should meet them (entry point first, helpers after, tests last) and choose Combine into one PDF. Every file starts on a new page with its own name in the header. The PDF outline, which PDF readers show as bookmarks in the sidebar, has one entry per file, and the contents page lists each file with its page number. Page numbers run through the whole document, so "page 14" means the same thing to everyone.
If you would rather have one long source file than one PDF with sections, join the files first with the TXT Merger, which accepts .py files and can insert a separator between files. Project notes written in Markdown can be joined with the Markdown Merger. If the submission also needs a saved web page, the MHTML to PDF converter turns an .mhtml archive into a PDF, and the PDF tools hub lists the rest.
Some converters take a screenshot of the code and wrap the picture in a PDF. The result looks right but is blurry when zoomed, large, and useless for search or copy and paste. This tool writes text operators instead, as defined in the PDF standard ISO 32000-2. Each run of characters is placed at an exact column, so the indentation that Python depends on is reproduced precisely.
The font is DejaVu Sans Mono, a free typeface with clear differences between 0 and O and between 1, l and I. Only the characters your code uses are embedded, together with a table that maps each glyph back to its Unicode character. That table is what lets a PDF reader search for def main or copy a function out again. To check a finished file, open it in any viewer from our list of the best PDF readers and editors for Mac or in your browser and try selecting a line.
If you need to convert Python code to PDF as part of a script or a build, the usual route is Pygments, the highlighter behind many documentation tools. Pygments does not write PDF itself. It writes HTML, which a renderer such as WeasyPrint turns into a PDF:
from pathlib import Path
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import PythonLexer
from weasyprint import HTML # pip install pygments weasyprint
source = Path("script.py").read_text(encoding="utf-8")
formatter = HtmlFormatter(
full=True, # a complete HTML page with the CSS included
linenos="inline", # line numbers in front of every line
title="script.py",
prestyles="white-space: pre-wrap; font-size: 9pt;", # wrap long lines
)
html = highlight(source, PythonLexer(), formatter)
HTML(string=html).write_pdf("script.pdf")
print("Saved script.pdf")Run it with a recent Python 3; the Python documentation covers pathlib and file encodings. Wrap the last lines in a loop over Path("src").rglob("*.py") to convert a whole folder. If scripting is new to you, our guide on how to parse JSON in Python uses the same file-reading pattern.
Long lines are only cut when Long lines is set to Truncate. Set it to Wrap, or give the lines more room with landscape orientation, narrow margins or a smaller font. The hint under the settings shows the longest line in your files next to the number of characters that fit.
Garbled accents mean the file is not saved as UTF-8. The converter reads a byte order mark and the coding comment described in PEP 263, for example # -*- coding: latin-1 -*-, and falls back to Windows-1252 when a file is not valid UTF-8. If the guess is wrong, re-save the file as UTF-8 in your editor and convert again. Boxes in place of characters mean the embedded font has no glyph for them, which is the case for Chinese, Japanese, Korean and emoji.
The file probably uses tab characters, and your editor shows a tab as a different width. Set Tab width to the value your editor uses. Files indented with spaces, as PEP 8 recommends, are not affected. A file that mixes tabs and spaces may look uneven at any setting, which is a good reason to fix the source.
Files over 10 MB and files that contain NUL bytes, such as compiled .pyc files, are rejected with a message for that file only. Large source files convert at roughly two thousand lines per second and show a progress bar with a Cancel button; files over 2 MB are printed without colors to keep the browser responsive. To look inside a huge generated file first, open it in the Large Text File Viewer, or cut it into parts with the TXT File Splitter and convert only the part you need.
Source code is often private: unreleased coursework, client work or scripts with internal host names. The converter reads your files with the browser's File API and builds the PDF in memory on your device. No code is uploaded, logged or executed. The only request the page makes during a conversion is for its own font files, which carry no information about your code. Still, remove passwords and API keys from a script before you share the PDF, because the text in it is searchable.
The TXT tools and guides hub collects everything for plain-text files. Write a quick script or note in the browser with the TXT File Maker, combine sources with the TXT Merger, and find more converters in the PDF tools hub.
Explore more PDF tools, save a web page as a PDF, or join text files before converting them.