DEV Community

pBinder
pBinder

Posted on

How to Convert Python Files to PDF or DOCX (Without Installing LaTeX)

You have a .py file — or a handful of them — and you need a PDF or Word document. Maybe you're submitting coursework, sharing code with a non-developer, creating a printable archive, or reviewing an open-source repo offline.

There are several ways to do this, each with different trade-offs. Here's an honest breakdown.


Method 1: Pandoc (Free, Open Source)

Pandoc is a mature, widely-used document converter that handles .py to DOCX or PDF well.

pandoc my_script.py -o output.docx
pandoc my_script.py -o output.pdf
Enter fullscreen mode Exit fullscreen mode

DOCX output works cleanly without extra dependencies — a solid choice if you're comfortable with the command line.

PDF output requires a separate PDF engine. By default Pandoc uses LaTeX — on Windows that means installing MiKTeX, on macOS it's MacTeX. Both are large installs (MiKTeX can reach several GB depending on packages). Pandoc also supports lighter alternatives like wkhtmltopdf or weasyprint via the --pdf-engine flag, but those also require separate installation. Once a PDF engine is set up, Pandoc is powerful and fully scriptable — you can batch-process entire directories and customise output with templates.

Best for: Developers comfortable with CLI who want a free, scriptable solution. DOCX output requires minimal setup; PDF requires choosing and installing a PDF engine first.


Method 2: Jupyter nbconvert (For Notebooks)

If you're working with .ipynb notebooks, nbconvert is the right tool. It handles cell outputs, inline plots, and markdown cells cleanly.

jupyter nbconvert --to html notebook.ipynb
jupyter nbconvert --to pdf notebook.ipynb
Enter fullscreen mode Exit fullscreen mode

Native output formats include HTML, PDF, Markdown, and script. DOCX is not a native nbconvert format — converting to DOCX requires Pandoc and python-docx installed separately (available in JupyterLab via File → Export As → Word, if those dependencies are present). PDF output requires a LaTeX distribution.

Best for: Data scientists and notebook users who need HTML or PDF exports. Not applicable to plain .py files.


Method 3: Sphinx + LaTeX (Full Documentation Build)

Sphinx is the standard Python documentation tool. It generates a full docs site from your source code, and make latexpdf exports a PDF.

The setup is substantial — you need Sphinx configured, docstrings in the right format, and a LaTeX install — but the output quality is genuinely excellent: proper cross-references, module indexes, and a professional layout.

Best for: Open-source projects that need real, maintained documentation. Too much overhead for a quick export, but the right long-term investment for serious projects.


Method 4: VS Code Print Extension or Browser Print

Open a file in VS Code with a print extension, or open it on GitHub and use browser print-to-PDF. No installs, no command line, works immediately.

Output quality is limited — margins and page breaks aren't controlled, long lines can get cut off, and there's no table of contents. For a short script it's perfectly fine.

Best for: A quick rough printout of a single file when appearance doesn't matter much.


Method 5: pBinder (Windows, Free Trial Available)

pBinder is a portable Windows EXE built specifically for converting Python files and projects to DOCX and PDF. It requires no LaTeX, no Python environment, and no installation — download and run.

It works for a single file, a handful of files, or an entire project directory. It's also useful beyond your own code: clone a GitHub repo you're studying and convert it to a navigable document to read offline or annotate.

What sets it apart from the other methods is the output structure. Rather than a flat printout, it produces a document you can navigate:

  • Clickable table of contents with a project folder tree
  • Symbol directory — every locally defined class, function, and method listed with a hyperlink to the page it's defined on
  • Call-site cross-references — wherever a local function or class is called in the code, that call site links back to the exact page and line where it's defined. Similar to how you'd navigate in an IDE, but inside a DOCX or PDF

The main limitations: Windows only (Windows 10 64-bit), and PDF export requires Microsoft Word. DOCX works without Word.

Best for: Windows users who want navigable, structured output from a Python project without the LaTeX and CLI setup that Pandoc or Sphinx require. A 14-day free trial is available at pbinder.app.


Comparison

Method DOCX PDF Multiple Files TOC LaTeX Required Setup
Pandoc Manual scripting PDF engine needed CLI
nbconvert For PDF Jupyter env
Sphinx Heavy
VS Code / Browser No None
pBinder No None

Which Should You Use?

One file, no fuss → VS Code or browser print

Notebooks → nbconvert

Scripting and DOCX output → Pandoc (no LaTeX needed for DOCX)

Full project documentation long-term → Sphinx

Navigable DOCX or PDF, no setup, Windows → pBinder

Every tool here has a legitimate use case. The right choice depends on your OS, how much setup you're willing to do, and what you need the output to look like.


Have a method I missed? Drop it in the comments.

Top comments (0)