PY to PDF Converter

Convert Python .py files to PDF with syntax highlighting and line numbers, directly in your browser with no upload.

Convert Python Code to a Clean, Readable PDF

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.

Line numbers and wrapping

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.

Batch and combine

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.

How to Convert a PY File to PDF

Your .py files are only read, never changed and never run.

1

Add your Python files

Choose or drop .py files, paste code, or load the sample script. Up to 100 files, 10 MB each.

2

Check the preview

Select a file to see it highlighted with line numbers. Reorder files with the arrows.

3

Pick the PDF settings

Set paper size, font size, theme, line numbers, long-line handling, header and footer.

4

Convert and download

Preview the finished PDF, then download it, or download a ZIP when you convert several files.

PDF Settings Explained

Each option changes how the Python code is laid out on the page.

Paper size and orientation
A4, Letter, Legal or A3, in portrait or landscape. Landscape fits about 50 percent more characters on a line, which helps code with long lines.
Margins
Narrow (0.5 in), Normal (0.75 in) or Wide (1 in). Wide margins leave room for handwritten comments and for hole punching.
Font size and line spacing
7 to 14 point, with compact, normal or relaxed spacing. At 9 point on A4 portrait a line holds 84 characters next to the line numbers.
Line numbers
Prints the source line number in a gutter. Wrapped rows are not numbered, so the numbers match your editor.
Long lines
Wrap continues a long line on the next row with an arrow marker and prefers to break after a space or punctuation. Truncate cuts the line at the margin and marks the cut.
Tab width
Tabs are expanded to the next tab stop every 2, 4 or 8 columns, the way an editor shows them. It has no effect on files indented with spaces.
Theme
Light uses colors that stay readable on white paper. Print-friendly is black and white with bold keywords and italic comments. Dark paints a dark page background.
Header and footer
The header shows the file name and an optional title such as your name. The footer shows Page X of Y and, if you want, today's date.
Output
One PDF per file gives each file its own document and a ZIP for several. Combine into one PDF starts each file on a new page and adds bookmarks and an optional contents page.
Output filename
Use {name} for the original file name, plus {index} or {ext} if you want numbered or extension-tagged names. A combined PDF is named python-code.pdf unless you change it.

Are My Files Uploaded?

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.

Can I Select and Search the Code in the PDF?

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.

Frequently Asked Questions

Complete Guide to Converting Python Code to PDF

When You Need Python Code as a PDF

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:

  • Assignments and exams. Many courses and learning platforms accept only PDF uploads, and markers want line numbers so they can write "line 42: off-by-one" in their feedback.
  • Code review on paper. Reading a tricky module away from the screen, with a pen, still finds bugs that scrolling misses.
  • Printing. A printed listing for a lab book, a whiteboard session or an interview.
  • Documentation and reports. An appendix with the exact script that produced a result, for example the plotting code behind a chart made with one of the Python data visualization libraries.
  • Portfolios and archives. A fixed copy of a script as it was on a given date, which anyone can open without an editor.

Ways to Convert a Python File to PDF Compared

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.

MethodSetupColorsLine numbersGood to know
This PY to PDF converterNone, runs in the browserYes, three themesYesWraps long lines, batch and combined output, selectable text. Chinese, Japanese, Korean and emoji characters are replaced with a box.
VS CodeAn extensionDepends on the extensionDepends on the extensionVS Code has no built-in print command, so you install an extension that opens the file in a browser and print from there.
PyCharmPyCharm installedYesYesFile > 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.
IDLEComes with PythonNoNoFile > Print Window sends plain text to the default printer, so you need a PDF printer set as default.
enscript or a2psLinux or macOS command lineYesYesProduces PostScript that you convert with ps2pdf. Fast for many files, but UTF-8 text beyond Latin-1 is poorly supported.
Pygments scriptPython plus two packagesYes, many stylesYesFlexible and scriptable. Pygments writes HTML or LaTeX, so a second tool turns that into a PDF.
Pasting into WordWord or Google DocsOnly if the editor copies themNoAutocorrect 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.

How to Convert PY to PDF With This Tool

  1. Choose or drop your .py files in the converter at the top of this page, or select Paste code.
  2. Select a file in the list to see the highlighted preview, and reorder files with the arrows.
  3. Pick the paper size your reader expects: A4 in most countries, Letter in the US and Canada.
  4. Read the line under the settings. It says how many characters fit on a line and how long your longest line is.
  5. Choose a theme, and decide whether you want line numbers, a header title and page numbers.
  6. Choose one PDF per file or one combined PDF, then select Convert to PDF.
  7. Check the preview, then select Download PDF, or Download ZIP when there are several files.

Choosing Font Size, Paper and Wrapping So Nothing Is Cut Off

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.

  • Lines up to 88 or 100 characters (the Black and Ruff defaults, or a common team limit): use narrow margins or 8 point.
  • Lines of 120 characters or more: switch to landscape, which fits about half as many characters again.
  • A few very long lines, such as URLs or data literals: leave Wrap on. The rest of the file keeps its shape and only those lines continue on a second row.
  • Reading comfort: 9 to 10 point prints well. Below 8 point the code is hard to read on paper, so prefer landscape over a tiny font.

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.

Combining a Project Into One PDF

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.

How the PDF Keeps Your Code Selectable

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.

Do It Yourself With Pygments

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:

Pythonpy_to_pdf.py
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.

Troubleshooting

Lines are cut off at the right edge

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.

Accented or non-English characters look wrong

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.

Indentation looks different from my editor

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.

A file is rejected or very slow

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.

Privacy

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.

More tools for text and code files

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.

Continue working with your files

Explore more PDF tools, save a web page as a PDF, or join text files before converting them.