If you've ever needed to share a Python script with someone who doesn't run Python — a teacher grading homework, a hiring manager reviewing a code sample, a non-developer client signing off on a deliverable — you've probably wondered: what's the cleanest way to turn a .py file into a PDF?
Plain text isn't great. The indentation looks off when pasted into Word. A screenshot only captures one screen-worth and you lose searchable text. And printing directly from a code editor is hit-or-miss — sometimes the colors transfer, sometimes they don't.
This guide walks through five reliable ways to convert Python code to PDF, from the fastest browser tools to professional-grade LaTeX output. Each method has a sweet spot, and by the end you'll know exactly which one to reach for in your situation.
Why convert Python to PDF in the first place?
Before we dive into the how, it's worth being clear about the why. The most common reasons developers and students reach for a .py-to-PDF converter are:
- Academic submissions. Many computer-science courses require homework as PDF, even if the source is Python. A converted file ensures the professor sees the indentation correctly and can annotate easily.
- Job applications and interviews. When attaching a code sample to an application, PDF guarantees the reviewer sees the file exactly as you intend — same font, same colors, same line numbers — regardless of their editor.
- Code reviews. Print-friendly PDFs with line numbers make in-person or async review more efficient. Reviewers can mark up the PDF directly with comments.
- Archiving. Source files change over time; a PDF snapshot captures a script exactly as it existed on a given date. Useful for compliance, audits, or historical reference.
- Teaching and presentations. Including code on a slide is fine for a few lines, but for a full module you want a clean, branded document you can hand out.
Whatever your reason, the key qualities you want in the output PDF are: preserved indentation, syntax highlighting, optional line numbers, monospaced font, and no quality loss. Let's see which tools deliver on those.
Method 1: Online converter (fastest, no install)
If you just need a PDF right now and you don't want to install anything, an online converter is the easiest path. FixMyPDF's Python to PDF tool takes a .py file and returns a syntax-highlighted PDF in seconds. You can choose a theme (light or dark), enable line numbers, and pick a font size.
The flow looks like this:
- Open fixmypdf.net/py-to-pdf in your browser.
- Drag your
.pyfile into the upload area. - Pick your formatting options (theme, line numbers, font size).
- Click "Convert to PDF".
- Download the result — no sign-up, no watermark.
Best for: students and developers who want a clean, professional-looking PDF in under a minute without installing anything.
Limitations: You're uploading source to a third-party server. If your code contains secrets or is under NDA, redact before upload — or use one of the offline methods below.
Method 2: Print from VS Code (with syntax highlighting)
VS Code has solid PDF export through extensions. The most popular is "PDF" by yzane, which converts open files (including .py) to PDF with proper syntax highlighting from your current theme.
- Install the "PDF" extension by yzane from the Marketplace.
- Open your
.pyfile in VS Code. - Press
Ctrl+Shift+P(orCmd+Shift+Pon Mac) and run "Markdown PDF: Export (pdf)". - The PDF appears next to the source file.
Pro tip: Set your editor theme to one with high contrast (the default Dark+ is fine) before exporting — the PDF will use the same theme colors.
Best for: developers who already live in VS Code and want offline conversion with their preferred theme.
Limitations: Slightly slower than online tools because of the extension overhead. The output is a "rendered" PDF, so very long files may break across pages awkwardly without manual tweaking.
Method 3: Use Pygments + WeasyPrint (Python-native)
If you want a fully programmatic, reproducible workflow — perhaps as part of a CI pipeline or batch job — you can do it with two Python packages:
pip install pygments weasyprint
# Convert a .py file to PDF
pygmentize -f html -O full,style=monokai script.py > script.html
weasyprint script.html script.pdf
This produces a beautiful PDF with full syntax highlighting and complete control over the output. You can customize fonts, page size, headers, line numbers, and themes by passing additional options to pygmentize or wrapping it in a small Python script.
Best for: engineers automating PDF generation as part of a build, deploy, or documentation pipeline.
Limitations: Requires installing two packages and writing a tiny script. Not the best choice for a one-off conversion.
Method 4: Jupyter Notebooks → PDF (for .ipynb sources)
If your Python source is actually a Jupyter notebook (.ipynb), you have a different and very polished workflow. From a notebook:
- Click File → Download as → PDF via LaTeX (.pdf).
- Jupyter renders the cells, outputs, plots, and Markdown into a typeset PDF.
This requires a working LaTeX installation (most easily installed via brew install --cask mactex on macOS or sudo apt install texlive-xetex on Ubuntu). The output is publication-quality and includes any inline plots from matplotlib or seaborn.
Best for: data scientists and researchers who want to share notebooks with non-Python users.
Method 5: LaTeX (publication-grade)
For the absolute highest quality output — academic papers, books, official reports — wrapping Python in LaTeX with the listings or minted package produces unbeatable typography.
\documentclass{article}
\usepackage{minted}
\begin{document}
\inputminted{python}{script.py}
\end{document}
Compile with pdflatex -shell-escape script.tex and you have a PDF that rivals anything in a textbook.
Best for: academic publication, formal technical documentation, theses.
Limitations: LaTeX has a steep learning curve. Not worth the setup unless you're already in a LaTeX workflow.
Quick comparison: which method should you use?
| Method | Speed | Quality | Setup | Best for |
|---|---|---|---|---|
| Online (FixMyPDF) | ⚡⚡⚡ | ★★★★ | None | Quick one-off conversions |
| VS Code extension | ⚡⚡ | ★★★ | 1 minute | Daily use, offline |
| Pygments + WeasyPrint | ⚡ | ★★★★ | 5 minutes | CI / batch jobs |
| Jupyter → PDF | ⚡⚡ | ★★★★★ | LaTeX install | Notebooks with outputs |
| LaTeX + minted | ⚡ | ★★★★★ | Significant | Academic publication |
Need a Python-to-PDF conversion right now?
Drop your .py file into our free converter — syntax highlighting, line numbers, no watermark, no sign-up.
Tips for great-looking Python PDFs
No matter which method you use, a few habits will keep your output looking professional:
- Use a monospaced font. Indentation matters in Python. Fonts like JetBrains Mono, Fira Code, Consolas, and Menlo all render code cleanly.
- Enable line numbers if reviewers will reference them. Especially helpful for code reviews where someone will say "look at line 42".
- Keep lines under 100 characters. Long lines either wrap awkwardly or get cut off in landscape mode. PEP 8 recommends 79; most teams use 100 or 120.
- Include a header with the file name and date. Especially useful for archived snapshots — someone reading the PDF six months from now will appreciate context.
- Choose light theme for printing. Dark themes look great on screen but waste a lot of ink. Switch to a light theme before exporting if the recipient is likely to print.
Frequently asked questions
Can I convert a .py file directly from my iPhone or Android?
Yes. Any of the online tools (including FixMyPDF's converter) work in mobile browsers. Just open the page, upload the file from your phone's file picker, and download the result. The whole workflow takes about 30 seconds on a phone.
Will my comments and docstrings be preserved?
Yes. Every method here preserves the full source code — including triple-quoted docstrings, hash-mark comments, and inline annotations. Comments get rendered in a distinct color so they stand out.
How do I convert multiple Python files at once?
For a one-time batch, you can use the Pygments approach in a shell loop:
for f in *.py; do
pygmentize -f html -O full,style=monokai "$f" > "$f.html"
weasyprint "$f.html" "${f%.py}.pdf"
done
Or, simpler: zip your files and use an online converter that accepts zip archives.
Is converting Python code to PDF safe for proprietary code?
Online converters upload your source to a server. For proprietary code, prefer offline methods (VS Code extension or LaTeX) or strip out any secrets/credentials before uploading. FixMyPDF automatically deletes uploaded files within an hour, but for highly sensitive code an offline tool is still the safer choice.
Wrap-up
Five paths, one outcome: a clean Python PDF you can share with confidence. For most readers of this article, the online converter will be the right choice — it's free, fast, and produces a professional-looking PDF without any setup. If you find yourself converting code regularly as part of your workflow, the VS Code extension or Pygments approach pays off quickly.
Got a Python file ready to convert? Try our Python to PDF tool now and see the difference proper syntax highlighting makes.