Code to PDF

How to Convert Python (.py) Files to PDF: 5 Methods Compared

A complete guide to printing or sharing Python source code as a PDF — from quick browser converters to professional approaches with LaTeX. Pros, cons, and use cases for each method.

H By Houcine Published May 12, 2026 8 min read

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:

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:

  1. Open fixmypdf.net/py-to-pdf in your browser.
  2. Drag your .py file into the upload area.
  3. Pick your formatting options (theme, line numbers, font size).
  4. Click "Convert to PDF".
  5. 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.

  1. Install the "PDF" extension by yzane from the Marketplace.
  2. Open your .py file in VS Code.
  3. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac) and run "Markdown PDF: Export (pdf)".
  4. 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:

  1. Click File → Download as → PDF via LaTeX (.pdf).
  2. 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?

MethodSpeedQualitySetupBest for
Online (FixMyPDF)⚡⚡⚡★★★★NoneQuick one-off conversions
VS Code extension⚡⚡★★★1 minuteDaily use, offline
Pygments + WeasyPrint★★★★5 minutesCI / batch jobs
Jupyter → PDF⚡⚡★★★★★LaTeX installNotebooks with outputs
LaTeX + minted★★★★★SignificantAcademic 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.

Open the Python to PDF tool →

Tips for great-looking Python PDFs

No matter which method you use, a few habits will keep your output looking professional:

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.

#python #py to pdf #developer tools #tutorial
Code to PDF

How to Convert Python (.py) Files to PDF: 5 Methods Compared

A complete guide to printing or sharing Python source code as a PDF — from quick browser converters to professional approaches with LaTeX. Pros, cons, and use cases for each method.

H By Houcine Published May 12, 2026 8 min read

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:

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:

  1. Open fixmypdf.net/py-to-pdf in your browser.
  2. Drag your .py file into the upload area.
  3. Pick your formatting options (theme, line numbers, font size).
  4. Click "Convert to PDF".
  5. 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.

  1. Install the "PDF" extension by yzane from the Marketplace.
  2. Open your .py file in VS Code.
  3. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac) and run "Markdown PDF: Export (pdf)".
  4. 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:

  1. Click File → Download as → PDF via LaTeX (.pdf).
  2. 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?

MethodSpeedQualitySetupBest for
Online (FixMyPDF)⚡⚡⚡★★★★NoneQuick one-off conversions
VS Code extension⚡⚡★★★1 minuteDaily use, offline
Pygments + WeasyPrint★★★★5 minutesCI / batch jobs
Jupyter → PDF⚡⚡★★★★★LaTeX installNotebooks with outputs
LaTeX + minted★★★★★SignificantAcademic 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.

Open the Python to PDF tool →

Tips for great-looking Python PDFs

No matter which method you use, a few habits will keep your output looking professional:

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.

#python #py to pdf #developer tools #tutorial