Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Converting DOCX to PDF using Python
import sys
import subprocess
import re
def convert_to(folder, source, timeout=None):
args = [libreoffice_exec(), '--headless', '--convert-to', 'pdf', '--outdir', folder, source]
process =, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout)
filename ='-> (.*?) using filter', process.stdout.decode())
if filename is None:
raise LibreOfficeError(process.stdout.decode())
def libreoffice_exec():
# TODO: Provide support for more platforms
if sys.platform == 'darwin':
return '/Applications/'
return 'libreoffice'
class LibreOfficeError(Exception):
def __init__(self, output):
self.output = output
if __name__ == '__main__':
print('Converted to ' + convert_to(sys.argv[1], sys.argv[2]))

This comment has been minimized.

Copy link
Owner Author

MichalZalecki commented Jul 8, 2017

Full setup Flask and Docker 🚢 Converting DOCX to PDF using Python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.