Pypandoc

1.17 · active · verified Thu Apr 09

Pypandoc is a Python wrapper for Pandoc, a powerful universal document converter. It simplifies the process of converting files between various formats such as Markdown, HTML, DOCX, PDF, LaTeX, ODT, and EPUB. The library is actively maintained, with the current version being 1.17, and it follows a regular release cadence to incorporate updates and improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to convert a Markdown file to PDF and a Markdown string to HTML using `pypandoc`. For PDF conversion, installing `pypandoc[tinytex]` simplifies LaTeX setup. If `pypandoc_binary` is not used, ensure Pandoc is installed on your system or use `pypandoc.download_pandoc()`.

import pypandoc
import os

# Ensure pandoc is available (optional if using pypandoc_binary or pypandoc[tinytex])
# pypandoc.download_pandoc() # uncomment if you installed pypandoc without binary and don't have pandoc

# Create a dummy markdown file
with open('input.md', 'w') as f:
    f.write('# Hello from Pypandoc\n\nThis is a test document.')

# Convert markdown file to PDF (requires pandoc and a LaTeX distribution like TinyTeX)
# For seamless PDF conversion, install with `pip install pypandoc[tinytex]`
pypandoc.convert_file('input.md', 'pdf', outputfile='output.pdf')
print("Converted input.md to output.pdf")

# Convert markdown string to HTML
html_output = pypandoc.convert_text('# Another Title\n\nSome **bold** text.', 'html', format='md')
print("Converted Markdown string to HTML:")
print(html_output)

# Clean up dummy files
os.remove('input.md')
os.remove('output.pdf')

view raw JSON →